Print Friendly and PDF
In C#, a variable represents a storage location. A variable has a type that determines what values can be stored in this variable. Because C# is a type-safe language, the C# compiler guarantees that values stored in variables are always of the appropriate type. The value of a variable is changed through the assignment operator and through the use of the ++ and -- operators. A variable must be definitely assigned before its value can be obtained: variables are either initially assigned or initially unassigned. An initially assigned variable has a well-defined initial value, while an initially unassigned variable has no initial value.
In C#, a variable represents a storage location. A variable has a type that determines what values can be stored in this variable. Because C# is a type-safe language, the C# compiler guarantees that values stored in variables are always of the appropriate type.

 The value of a variable is changed through the assignment operator and through the use of the ++ and -- operators. A variable must be definitely assigned before its value can be obtained: variables are either initially assigned or initially unassigned. An initially assigned variable has a well-defined initial value, while an initially unassigned variable has no initial value.
Variables In C#

CATEGORIES OF VARIABLES IN C#

C# has seven categories of variables: static variables, instance variables, array elements, value parameters, reference parameters, output parameters, and local variables. The following sections describe each of these categories.

Static Variables

A variable, declared with the static keyword, is a static variable. The initial value of a static variable is the default value of the variable’s type. A static variable is initially assigned.

Instance Variables

A variable declared without the static keyword is an instance variable. An instance variable of a class exists when a new instance of that class is created and ceases to exist when there are no references to that instance and the instance’s destructor (if any) has executed. The initial value of an instance variable of a class is the default value of the variable’s type. An instance variable of a class is initially assigned.

Array Elements

Array elements exist when an array instance is created and cease to exist when there are no references to that array instance. The initial value of each of the elements of an array is the default value of the type of the array elements. An array element is initially assigned.

Local Variables

A local variable is declared within a block, a for-statement, a switch-statement, or a using-statement. The lifetime of a local variable is implementation-dependent. For example, the compiler could generate code that results in the variable’s storage having a shorter lifetime than its containing block. A local variable is not automatically initialized and it has no default value. A local variable is initially unassigned. It is a compile-time error to refer to the local variable in a position that precedes its declaration.

Value Parameters

A parameter declared without a ref or out modifier is a value parameter. A value parameter is initially assigned.

Reference Parameters

A parameter declared with a ref modifier is a reference parameter. A reference parameter represents the same storage location as the variable given as the argument in the function member invocation. Therefore, the value of a reference parameter is always the same as the underlying variable. A variable has to be definitely assigned before it can be passed as a reference parameter in a function member invocation. A reference parameter is considered initially assigned to a function member.

Output Parameters

An output parameter is a parameter declared with an out modifier. An output parameter represents the same storage location as the variable given as the argument in the function member invocation. Therefore, the value of an output parameter is always the same as the underlying variable. A variable does not need to be definitely assigned before it can be passed as an output parameter in a function member invocation. An output parameter is initially unassigned within a function member.

zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: