Csharp Variables
# C# Variables
A variable is simply a name for a storage area that the program can manipulate.
In C#, variables are identifiers used to store and represent data. When declaring a variable, you need to specify the type of the variable and optionally assign an initial value to it.
In C#, each variable has a specific type, which determines the memory size and layout of the variable, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.
We have already discussed various data types. The basic value types provided in C# can be broadly categorized as:
| Type | Examples |
| --- | --- |
| Integer types | sbyte, byte, short, ushort, int, uint, long, ulong, and char |
| Floating-point types | float, double |
| Decimal type | decimal |
| Boolean type | true or false values, specified values |
| Null string | string |
| Nullable type | A data type that can hold a null value |
C# allows defining variables of other value types, such as **enum**, and also allows defining reference type variables, such as **class**. These will be discussed in later chapters. In this chapter, we will only study the basic variable types.
C# 4.0 introduced the dynamic type, which allows the type of a variable to be inferred at runtime. This can be useful in some special cases, but it's generally better to use static typing for better performance and compile-time type checking.
dynamic dynamicVariable = "This can be any type";
## Variable Definition in C#
The syntax for variable definition in C#:
;
Here, data_type must be a valid C# data type, which can be char, int, float, double, or any other user-defined data type. variable_list may consist of one or more identifier names separated by commas.
Some valid variable definitions are shown below:
int i, j, k;char c, ch;float f, salary;double d;
You can initialize a variable at the time of definition:
int i = 100;
### Variable Naming Rules
In C#, variable naming must follow certain rules:
* Variable names can contain letters, digits, and underscores.
* Variable names must start with a letter or an underscore.
* Variable names are case-sensitive.
* Avoid using C# keywords as variable names.
int myVariable = 10;string _userName = "John";
## Variable Initialization in C#
Variables are initialized (assigned a value) by using an equal sign followed by a constant expression. The general form of initialization is:
variable_name = value;
Variables can be initialized at the time of declaration (specifying an initial value). Initialization consists of an equal sign followed by a constant expression, as shown below:
= value;
Some examples:
int d = 3, f = 5; /* Initialize d and f. */byte z = 22; /* Initialize z. */double pi = 3.14159; /* Approximate value of pi */char x = 'x'; /* The value of variable x is 'x' */
Correctly initializing variables is a good programming practice; otherwise, the program may sometimes produce unexpected results.
Look at the following example, which uses variables of various types:
## Example
using System;
namespace VariableDefinition
{
class Program
{
static void Main(string[] args)
{
short a;
int b ;
double c;
/* Actual initialization */
a =10;
b =20;
c = a + b;
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result:
a = 10, b = 20, c = 30
## Accepting Input from Users
The **Console** class in the **System** namespace provides a function **ReadLine()** for receiving input from the user and storing it in a variable.
For example:
int num; num = Convert.ToInt32(Console.ReadLine());
The function **Convert.ToInt32()** converts the user input data to the int data type, because **Console.ReadLine()** only accepts data in string format.
## Lvalues and Rvalues in C#
Two types of expressions in C#:
1. **lvalue**: An lvalue expression may appear on the left as well as right side of an assignment operator.
2. **rvalue**: An rvalue expression may appear on the right side of an assignment operator, but not on the left side.
Variables are lvalues and hence may appear on the left side of an assignment operator. Numeric literals are rvalues and hence may not be assigned, cannot appear on the left side. Following is a valid statement:
int g = 20;
Following is an invalid statement and will produce a compile-time error:
10 = 20;
YouTip