C# Variables


In C# variables represent storage locations. All variables in C#  has a type. Local variables are variables that are declared in methods, properties etc. We declare variables in C# using the following syntax:

datatype identifier;




for example;

     int mValue;





This statement declares an  int named mValue. The compiler won`t actually us use this variable until we have initialized it with a value, but the declaration allocates four bytes on the stack to hold the value.
Once it has beed declared, we assing a value to the variable using the assignment operator,  =;
    
mValue = 20;






We can also declare the variable and initialize its value at the same time:

    int mValue = 20 ;





This syntax is identical to C++ and Java syntax, but very different from Visual Basic for declaring variables.
If we declare and initialize more than one variable in a single statement, all of the variables will be of the same of data types:

     int mValue_I = 10, mValue_II = 20;





To declare variables of different types, you need to use separate statements. Dont`t assing different data types within a multiple variable declaration:

  int mValue = 20;
  bool m = true;//Creates a variable that stores true or false
  int mValue = 20 , bool m = true;   

No comments:

Post a Comment