Monday, January 4, 2010

Basic Syntax in c# Language



A variable is a location in the memory that has a name and contains a value. Variable means the value can be changed. A variable name may contains ‘_’ (underscore) followed by a sequence of letters, digits(0-9), or underscores. The first character in a variable name cannot be a digit

//Variable Declaration
[data_type] [variable_name]=[value];

//Data types are two types.
Value types:- The values are directly containing the data.
Char, int and float are value types

Example:

int a = 10; // 'a' is a variable that contains the values 10.
char c=’S’; // 'c' is a character variable that contains the value 'S'.
float f=10.22; // 'f' is a float variable that contains the value 10.22

Reference types:- The reference types do not maintain the data but they contain reference to the variables.
String is a reference types

Example:

string siteName= "dotnetpgm.blogspot.com"; //string is a set of character.

Looping statements in c#

1. The for loop
2. The while loop
3. The do...while loop

The for loop structure is used to execute a block of statements for a specific number of times.

//for loop for 5 times iteration

int index = 0;
for ( int index = 0; index < 5; index++ )
{
Response.WriteLine(index);
}

//for each is used for array of collection
string[] str = new string[]{ "dotnetblog", "dotnetpgm", "c#" }; //split the string by string by string

foreach(string c in str)
{ Response.WriteLine(c);
}

//while is used to check if the condition is true

int index = 0;
while ( index < 5 )
{
Response.WriteLine(index); ++index;
}

//do while is used to execute the code and then check the condition(only difference between with while)
int index = 0;
do
{
Response.WriteLine ( index );
index++;
}
while ( index < 5 );

Using Conditional Constructs Conditional is used for decision making. The going path is right means we will go if wrong means we will stop there and analyse and then go in the right path. Like this

//the value is true
bool value=true;
if(value)
{
Response.WriteLine("Yes the value is True");
}
else
{
Response.WriteLine("No the value is False");
}

//switch is used for more conditions but one condition will execute
int index=3;
switch(index)
{
case 1:
Response.WriteLine("Index value is "+index);
break;
case 2:
Response.WriteLine("Index value is "+index);
break; case 3: Response.WriteLine("Index value is "+index);
break;
} //the break will quit if the condition execute.

3 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai Dot Net Training in Chennai


    Dot Net Online Training Dot Net Online Training LINQ Online Training LINQ Online Training ASP.NET Online Training ASP.NET Online Training

    ReplyDelete
  2. The basic syntax in C# language is explained well My sincere thanks for sharing this post Please continue to share this post
    Dot Net Training in Chennai

    ReplyDelete

Followers