C# series 1.2 : Variables - ponda

Breaking

6/05/2017

C# series 1.2 : Variables

Variables are storage locations in the memory

C# is a type safe language, i.e. every variable has a type associated with it which will determine what type of value to be stored. The value of the variable can be changed using the assignment operator or by using ++ and -- operator.

Syntax :
<visibility> <data type> <name> = <value>;

Example :
public string name = "Avinash";

There are seven categories of variables :
  1. static variable
  2. instance variables
  3. array elements
  4. value parameters
  5. reference parameters
  6. output parameters
  7. local variables
Following example will cover all of the above variables :
Example :

class A
{
     public static int x;
     int y;
     void F(int[] v, int a, ref int b, out int c)
     {
          int i = 1;
          c = a + b++;
     }
}















Program typically use data to perform tasks. Creating a variable reserves a memory location, or a space in memory, for storing values. It is called variable because the information stored in that location can be changed when the program is running.




To use a variable, it must first be declared by specifying the name and data type. A variable name, also called an identifier, can contain :

  • letters
  • numbers
  • underscore character ( _ ) 
and must start with a letter or underscore.

Although the name of a variable can be any set of letters and numbers, the best identifier is descriptive of the data it will contain. This is very important in order to create clear, understandable and readable code !

For example, firstName and lastName are good descriptive variable names, while abc and xyz are not.


Next Lesson : C# series 1.3 : Variables Types

No comments:

Post a Comment