Slide 8.7: Data types (cont.)
Slide 8.9: Variables (cont.)
Home

Variables


Variable Names
The following are the rules when naming the variables in C#: Examples of valid and invalid variable names are displayed as below: Declaring Variables
Syntax for variable definition in C# is as follows:
   <data_type>   <variable_list>;
Here, data_type must be a valid C# data type including char, int, float, double, or any user-defined data type, and variable_list may consist of one or more identifier names separated by commas:
int i, j, k;
char c, ch;
float f, salary;
double d;

Initializing Variables
Variables are initialized (assigned a value) with an equal sign followed by a constant expression:

   variable_name = value;
Variables can be initialized in their declaration:
   <data_type> <name> = value;
int d = 3, f = 5;
byte z = 22;
double pi = 3.14159;
char x = 'x';

Data Declarations
    String  my_Str = @"ASP.NET";        //  correct    incorrect
    bool   my-Bool = False;             //  correct    incorrect
    object     obj = 10;                //  correct    incorrect
    float  floatNo = 1F;                //  correct    incorrect
    short   short# = 1;                 //  correct    incorrect
    dynamic   this = 20;                //  correct    incorrect
    double      d1 = 1.0D;              //  correct    incorrect
Result: