Understanding C# Variables
THEOLUCHIJUNE
Posted on May 27, 2021
Understanding variables and how they work is very essential for every coder as variables are basic concepts which you see and make use of at any stage in coding.
In this article, you would know what C# variables are, the various data types that exist in C#, how to declare (create) variables and how to assign values to already declared variables.
What are variables?
Variables are containers for storing data values. All programs use data values for performing tasks.
It is called a variable because the data stored in this container can be changed when the program is running.
A variable name, also called an identifier can be any set of letters and numbers but the best variable name describes the type of data it contains.
For example, fullName and Age are adequate and descriptive variable names, while abc and 123 are not.
The general rules for constructing names for variables (unique identifiers) are:
- Names can contain letters, digits and the underscore character (_)
- Names must begin with a letter
- Names should start with a lowercase letter and it cannot contain whitespace
- Names are case sensitive ("myVar" and "myvar" are different variables)
- Reserved words (like C# keywords, such as int or double) cannot be used as names
-W3Schools in C# Variables
Data Types
A data type describes the type of data that can be stored in a variable or the type of data a variable can hold.
In C#, there are various data types, for example:
int : stores integers (whole numbers) from -2,147,483,648 to 2,147,483,647
double: stores fractional numbers. The double data type is sufficient for storing 15 decimal digits.
char: stores a single character/letter. It is surrounded by single quotes.
string: stores a sequence of characters. It is surrounded by double quotes.
bool: stores true or false values
How to Declare (Create) Variables
To declare a variable, you must specify the data type and the variable name.
Syntax
type variableName;
Note: The above syntax is a statement and must always end with a semicolon (;).
For example,
To declare a variable called name
of type string
,
string name;
This shows that the variable called name
stores text.
To declare a variable called age
of type int
.
int age;
This shows that the variable called age
stores integers.
Assigning Values
You can also assign a value to the variable you have declared.
There are two ways of assigning values to already declared variables. They are:
- Assigning values as you declare a variable,
type variableName = value;
For example, create a variable called name
of type string
and assign it the value Oluchi
:
string name = “Oluchi”;
- Assigning values after you declare a variable,
type variableName;
variableName = value;
For example, create a variable called name of type string and assign it the value Oluchi
:
string name;
name = “Oluchi”;
Not all values are surrounded by double quotes.
If you take a look at the data types stated earlier, you would notice that:
- the
string
data type is surrounded with double quotes - the
char
data type is surrounded with single quotes - the
int
data type isn’t surrounded with any quotes and so on.
Exercise: Create a variable called age
of type int
and assign it the value 51
.
Assign this value using the two ways of assigning values to variables.
Answer
int age = 51;
OR
int age;
age = 51;
Note: If you assign a new value to an existing variable, the new value would overwrite the existing value:
For example,
Change the value of age
to 35:
int age = 51;
age = 35;
The variable age
is now 35.
It is important that you try out various exercises on declaring variables and assigning values because it is only with constant practice that you get to understand how variables work better.
Happy Practising!
References
- W3Schools in C# Variables
- Cover image and Keyboard image from Raw Pixel
Posted on May 27, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.