What Does Variable Mean?
A variable, in C#, refers to a location in memory in which an application can store its data. Variables are used to store the result of calculations and hold the values that can change during the execution of a program. Variables are also used to place and retrieve the data forms an expression.
C# language is designed to be "type-safe," which helps ensure that the value stored in a variable is of the appropriate type. The type of a variable specifies what kind of data it can hold. This feature helps to reduce the burden from the programmer by guaranteeing the data's type safety.
Unlike loosely typed languages such as Jscript, C# expects the data type of a variable to be specified during declaration, which helps to allocate the memory for the variable during run time. In order to maintain the integrity of the data stored in a variable, C# defines a set of rules that dictate the permissible operations that can be performed on the variable.
Techopedia Explains Variable
A variable has to be declared before it is used. The declaration of a variable indicates its name, its type and an optional initial value. It is a good programming practice to assign a variable initially. A variable can be set to a value by an assignment or by using increment /decrement (++/--) operators. The scope of a variable determines its visibility to the program code and can be specified at the level of a class or method, or in nested code.
A variable can be a value or reference type. While variables of value type are stored on the stack, reference type variables are created on the heap with the reference to the allocated memory stored in the stack.
For example, a variable, "StudentName," can be declared as a string variable of reference type.
A variable that has been declared with a specific type cannot be redeclared with a new type. A variable of a particular type can be converted to another type using implicit type conversion or explicit conversion (cast). While implicit conversion does not incur any loss of data and occurs during compile-time, cast might cause data loss during run time.