C# Variables

C# Variables are used to represent storage locations. In C# variables have a particular type which determines what type of values can be stored in that variable. As C# is strongly typed language so we have to define a variable with the required data type. It will specify the type of data that has to behold in the variable.

Syntax for Defining Variable:

Characteristics of C# Variable:

Given below are some characteristics of the C# variable.

  1. Name: Name must be a valid identifier.
  2. Value: Value is the actual data that is to be stored in the variable.
  3. Data Type: Data Type defines the types of information which is to be stored into the variable.

Initializing C# Variables:

When we assign a value to a variable it’s known as Variable initializing. We can initialize value with the declaration or separately. Value assigns to the variable using (=) sign.

Syntax:
Examples:
There are 2 ways of Variable initialization.
  1. Run Time Initialization
  2. Compile Time Initialization

Run time Initialization

In run time initialization, there is one more possibility in which value is assigned to variable after completion of a function call. tIn this the user has to enter the value of a variable, and that value is copied to the required variable.

Compile Time Initialization

Compile-time Initialization is used to provide the value to the variable during the compilation of the program. It also has the default value option. In case, the programmer didn’t provide any value then the compiler will provide some default value to the variables. Compile-time initialization is helpful when the programmer wants to provide some default value.

Rules for Naming Variable:

Given below are some rules to define the name of C# variables.

  1. The variable name can’t start with a digit.
  2. The variable names can contain the letters or digits 0-9 as well as the character.
  3. The variable name can’t be any C# keyword say int, float, null, String, etc.
Example:

Given below are some examples of variable initialization.

(initializing variable t)

int t = 5;   

(initializes variable m)

float m = 2.12;   
      
(variable a has the value 'A')


char a = 'A';        

Types of Variables in C#:

Given below are several types of variables in C#:

  1. Local Variables
  2. Constant Variables
  3. Read-only Variables
  4. Static Variables / Class Variables
  5. Instance Variables / Non – Static Variables

Discuss them with examples:

1- Local Variables:

The local variable can be defined within a Constructor or block. When we declare the variables, these variables can be managed only within the block.

using System;
public class LocalVariable
{
public void GetAge()
{
int Student_Marks=45;         // local variable
Student_Marks= Student_Marks+34;
Console. WriteLine("Student's Total Marks = "+ Student_Marks);
}
public static void Main(String[] args)
{
LocalVariable _StudentObj=new LocalVariable();
_StudentObj.GetAge();
}
}

2- Constants Variables:

Constant variables are once initialized and the one-time life cycle of a class. It doesn’t need the instance of the class for initializing or accessing. We use the ‘const’ keyword to declare the constant variable. They can’t be changed after declaration.

using System;
public class ConstantVariables
{
int a=  74; // instance variable
static int b= 47; // static variable
const float maxValue =120; // constant variable
public static void Main()
{
ConstantVariables classObject= new ConstantVariables(); // object creation
Console.WriteLine("The Value of a = " + classObject.a);
Console.WriteLine("The Value of b = " + ConstantVariables.b);
Console.WriteLine("The Value of max = " + ConstantVariables. maxValue);
}
}

3- Read-only Variables:

The read-only variables can’t be changed after declaration just like constant variables. We use the ‘read-only’ keyword to declare the read-only variables. These variables can be initialized under the constructor. The Read-only is a permanent value for a specific instance of a class whereas The constant variable is an unchanging value for the entire class.

using System;
public class ReadonlyVariables
{
const float maxValue = 57; // constant variable
readonly int a; // read-only variable
public static void Main()
{
ReadonlyVariables classObject= new ReadonlyVariables(); // object creation
Console.WriteLine("The value of max: " + ReadonlyVariables. maxValue);
Console.WriteLine("The value of a : " + classObject.a);
}
}

4- Static Variables / Class Variables:

Static variables (also known as class variables) are defined as the variable which is created at the beginning of the program execution and destroyed at the end of the program execution.

using System;
public class Student
{
static int StudID;
static string StudName="John";
public static void Main(String[] args)
{
Student.StudID=12;  // accessing the static variable
Console. WriteLine(Student.StudName+ "'s ID=" + Student.StudID);
}
}

 5- Instance Variables or Non – Static Variables

Instance variables (also known as the non-static variables) are the instance variables that are declared in a class but declared outside of any method or constructor. These variables are created once the object of a class is created and it will destroy when the object becomes destroyed.

using System;
public class PatientDisease {
// instance variables
string Disease1;
string Disease2;
string Disease3;
public static void Main(String[] args) // Main Method
{
PatientDisease  pat1 = new PatientDisease  ();  //Object creation 1
pat1. Disease1 = "Cough";
PatientDisease  pat2 = new PatientDisease  (); //Object creation 1
pat2. Disease2 = "Flu";
PatientDisease  pat3 = new PatientDisease  (); //Object creation 1
pat3. Disease3 = "Fever";

Console.WriteLine("First Patient Disease= ");
Console.WriteLine(pat1. Disease1);
Console.WriteLine("Second Patient Disease= ");
Console.WriteLine(pat2. Disease2 );
Console.WriteLine("Third Patient Disease= ");
Console.WriteLine(pat3. Disease3 );

}
}

Types of Expressions in C#:

There are 2 kinds of expressions in the C# language:

1- lvalue − lvalue expression may appear as either the left-hand or right-hand side of an assignment. Such as Variables are lvalues because they may appear on the left-hand side of an assignment.
2- rvalue − rvalue expression may appear on the right- but not left-hand side of an assignment. Numeric literals are rvalues because they may not be assigned and can not appear on the left-hand side.
A valid C# statement:
int i = 15;
Not A valid C# statement:
35 = 45;