DEV Community

Cover image for Understanding C# Variables
THEOLUCHIJUNE
THEOLUCHIJUNE

Posted on • Updated on

Understanding C# Variables

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.

Alt Text

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; 
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

This shows that the variable called name stores text.

To declare a variable called age of type int.

int age;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

For example, create a variable called name of type string and assign it the value Oluchi:

string name = Oluchi;
Enter fullscreen mode Exit fullscreen mode
  • Assigning values after you declare a variable,
type variableName;
variableName = value;
Enter fullscreen mode Exit fullscreen mode

For example, create a variable called name of type string and assign it the value Oluchi:

string name;
name = Oluchi;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

OR

int age;
age = 51;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)