In the C# programming language, variables are named areas of memory used to store and manipulate data during program execution. Variables support the dynamic nature of a program and can hold values of different data types. Each variable has a specific data type, which determines what type of data the variable can hold and what operations can be applied to that data.
When defining variables, the data type and variable name are specified; for example, int age = 25; creates an integer variable named age and assigns it the value 25. In C#, variables are named using the camelCase style, usually starting with lowercase letters. Choosing correct and meaningful variable names makes the code easier to read and maintain. The scope and lifetime of variables are limited by the location and block where they are defined, which plays an important role in the memory management of the program.
Examples
//Value Types
//Console.WriteLine(“Hello World!”);
double number5 = 10.4; //can hold 15/16 values after the comma
decimal number6 = 10.5m; //can hold 28/29 values after the comma, the m at the end can be lowercase or uppercase
char character = 'A'; // string type is a char array ! single quote for char
bool condition = false;
byte number4 = 255;
short number3 = -4567;
int number1 = 234567896;
long number2 = 34567890346789678;
var number7 = 10; //takes whatever is assigned in the var type as the value type,
number7 = 'A'; //if we say the letter A is int, it will write 65 instead of A
//number7 = “A”; gives an error because it is a string
Console.WriteLine(“Number1 is {0}”, number1);
// We don't print the inside of the curly brackets as a string expression, we select the index from the expressions after the comma and print it
Console.WriteLine(“Number2 is {0}”, number2);
Console.WriteLine(“Number3 is {0}”, number3);
Console.WriteLine(“Number4 is {0}”, number4);
Console.WriteLine(“Number5 is {0}”, number5);
Console.WriteLine(“Number7 is {0}”, number7);
Console.WriteLine(“Character is {0}”, character);
Console.WriteLine(“Character is {0}”, (int)character);
//every value on the keyboard has a numeric equivalent
Console.WriteLine(Days.Friday);
Console.WriteLine((int)Days.Friday);
//value of the enum constants (this value can be changed instead of the default index)