C# variables are how you store data in your program so you can use it later. You give each variable a name so that you can remember what it keeps and access its value by using that name. When creating a variable, specify the data it will store. The data type determines how much space the variable takes up in memory and how the bits that make up the value are interpreted.
C# supports many data types, including numbers, characters, Boolean values (true or false), and strings (text). You can also create custom data types by defining your classes. Once you’ve declared a variable, you can assign a value using the equal sign (=).
For example, this code set creates an integer variable called myNumber and assigns the value 42 to it:
int myNumber = 42;
Assigning a value of the wrong data type to a variable, you’ll get an error.
For example, assigning a string to an integer variable, you’ll get a “Cannot implicitly convert type string to int” error.
To fix this, you can use a typecast which converts the data type of the value explicitly:
int myNumber = (int)”42″; // Convert the string “42” to an integer.
Characteristics Of C# Variables
Name:
The name must be a valid identifier.
Type:
It specifies the types of data that will be stored in the variable.
Value:
It specifies the actual data stored in the variable.
Rules For Defining Variables
A variable can contain alphabets, numbers, and underscores.
Only the alphabet and underscore can be used to begin a variable name. It cannot start with a digit.
Within the variable name, no white space is permitted.
A variable name cannot contain reserved words or keywords like char, float, etc.
For instance, for Valid Variable Names, you will have something like
However, for invalid variable names, you will have something like
Creating Variables
To make a variable, you must first specify its type and then assign it a value:
Where type is a C# type (for example, int or string) and variableName is the variable’s name (such as x or name). The equality sign is used to assign values to variables.
Consider the following example that creates a variable storing a number:
Without assigning a value, you can declare a variable and then set it later:
Initializing Variables
The term initializing refers to the process of assigning a value to a variable. Initialization can be done independently or in conjunction with a declaration. The actual use of variables falls under the initialization section. In C#, each data type has a default value used when a variable’s value is not explicitly set.
Initializing variables look like this:
You should know that initializing variables can be done in two ways: Compile Time Initialization and Run Time Initialization.
Compile Time Initialization
It means to assign a value to the variable during program compilation. Sometimes, the compiler will give a default value to the variables if the programmer does not provide a value. This initialization is useful when the programmer wants to provide a default value.
This type of initialization should look something like this:
// C# program to demonstrate the // Compile Time Initialization using System; class Geeks { // only declaration, compiler will // provide the default value 0 to it int y; // Main Method public static void Main(String []args) { // Compile Time Initialization of variable 'x' // Assigning value 32 to x int x = 32; // printing the value Console.WriteLine("Value of x is "+x); // creating object to access // the variable y Geeks gfg = new Geeks(); // printing the value Console.WriteLine("Value of y is "+gfg.y); } }
The output will be something like this:
Run Time Initialization
The user enters the value, which is then copied to the required variable. A possibility for Run Time initialization is; a value is assigned to a variable after completing a function call.
Run time Initialization should be something like this:
// C# program to demonstrate the // Run Time Initialization using System; class Geeks { // Main Method public static void Main(String []args) { // Value will be taken from the user // input and assigned to a variable // num int num = Convert.ToInt32(Console.ReadLine()); // printing the result Console.WriteLine("Value of num is " + num); } }
Variables in C# are classified based on how they store their value in memory. Variables are classified as value type, reference type, or pointer type.
When declaring variables, it is not necessary to specify the specific type. Instead of a data type, use the var keyword. Could you find out more about it next?