variable in c# language

Variable in C# Language-: Like any other programming language c#  also support many variable.Variables represent storage locations. Every variable has a type that determines what values can be stored in the variable. C# is a type-safe language, and the C# compiler guarantees that values stored in variables are always of the appropriate type. The value of a variable can be changed through assignment or through use of the ++ and — operators.

variables are either initially assigned or initially unassigned. An initially assigned variable has a well-defined initial value and is always considered definitely assigned. An initially unassigned variable has no initial value. For an initially unassigned variable to be considered definitely assigned at a certain location, an assignment to the variable must occur in every possible execution path leading to that location

Variable Category-: C# defines seven categories of variables:

  • static variables-:A field declared with the static modifier is called a static variable. A static variable comes into existence before execution of the static constructor (§10.12) for its containing type, and ceases to exist when the associated application domain ceases to exist.
  • Instance variables-: A field declared without the static modifier is called an instance variable. this is the two type : Instance Variables in Classes-:An instance variable of a class comes into existence 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 (§5.2) of the variable’s type.Instance Variables in StructsAn instance variable of a struct has exactly the same lifetime as the struct variable to which it belongs. In other words, when a variable of a struct type comes into existence or ceases to exist, so, too, do the instance variables of the struct.
  • array elements-: The elements of an array come into existence 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 (§5.2) of the type of the array elements.
  • value parameters-:A parameter declared without a ref or out modifier is a value parameter.
  • reference parameters-: A parameter declared with a ref modifier is a reference parameter.A reference parameter does not create a new storage location. Instead, a reference parameter represents the same storage location as the variable given as the argument in the function member or anonymous function invocation. Thus the value of a reference parameter is always the same as the underlying variable.
  • output parameters-:A parameter declared with an out modifier is an output parameter.An output parameter does not create a new storage location. Instead, an output parameter represents the same storage location as the variable given as the argument in the function member or delegate invocation. Thus the value of an output parameter is always the same as the underlying variable
  • local variables. -: A local variable is declared either by a local-variable-declaration, which may occur in a block, a for-statement, a switch-statement, or a using-statement; by a for each-statement; or by a specific-catch-clause for a try-statement.

in other words we can say that the Variables represent storage locations, and every variable has a type that determines what values can be stored in the variable, as shown by the following table.

 

c# variable

Basic type variable are the

  • Int
  • float
  • decimal
  • Boolean
  • Nullable

Define  a variable -:  when we are using variable in c# programming language we must define a variable in the programming as like

Int  i, j,k;

Char ram,shyam;

Float f, salary;

by this way we can define a variable.

Rules for defining a variable name-: Like any other programming language c# has some rule’s for declare a variable ..

  • A variable must have alphabets, digits and underscore.
  • A variable name can start with alphabet and underscore only. It can’t start with digit.
  • No white space is allowed within variable name.
  • A variable name must not be any reserved word or keyword e.g. int,char, float etc.

 

Initializing Variables  Name: After define a variable name we can Initializing variable in our programme as like

variable  variable_name;

varaible example c#

Output of this programme

variable output c#

The Console class in the System namespace provides a function ReadLine() for accepting input from the user and store it into a variable

int ram;

ram= convert.Toint32(console.write));

The function Convert.ToInt32() converts the data entered by the user to int data type, because Console.ReadLine() accepts the data in string format.

 

Leave a Comment