Related Posts with Thumbnails

C# Language Syntax

Monday, May 3, 2010

C# Language Syntax and essentials 


his article will show you the basic statements in C# language and language syntax.

Declaring Variables





The following sample shows different ways you can declare a variable:
int a;
int salary, incomeTax, sum;
int count = 10;

string name;
string fullName= "Little John";

Loop Statements



while
int i = 0;
while ( i < 5 )
{
Console.WriteLine ( i );
++i;
}

The above loop repeates 5 times and prints the value of i.
The output of above code would be like this :
0
1
2
3
4


for
for ( int i = 0; i < 5; i++ )
{
Console.WriteLine ( i );
}

The above loop repeates 5 times just like the while loop and prints the value of i.
The output of above code would be like this :

0
1
2
3
4


do ... while

int i = 0;
do
{
     Console.WriteLine ( i );
     i++;
}
while ( i < 5 );

The above loop is pretty much same as the while loop. The only difference is, the condition is checked only after executing the code inside the loop.

foreach

string []  names = new string[]{ "Little John", "Pete", "Jim", "Bill" };
foreach ( string name in names )
{
         Console.WriteLine ( name );
}


foreach loop can be used to iterate through a collection like array, ArrayList etc.
The above code displays the following output:

Little john
Pete
Jim
Bill


Conditional Operators




if ... else

This is the conditional operator, used to selectively execute portions of code, based on some conditions.

string name = "Little John";
if ( name == "Jim" )
{
     Console.WriteLine( "you are in 'if' block" );
}
else
{
     Console.WriteLine( "you are in 'else' block" );
}

in the above case, it prints :

you are in 'else' block

Flow Control Statements




break

'break' statement is used to break out of loops ('while', 'for', switch' etc).

string []  names = new string[] { "Little John", "Pete", "Jim", "Bill" };
foreach ( string name in names )
{
     Console.WriteLine ( name );
     if ( name == "Pete" )
      break;
}

In the above sample, it iterates through the array of 4 items, but when it encounters the name "Pete", it exits the loop and will not continue in the loop anymore.
The output of above sample would be :

Little John
Pete


continue

'continue' statement is also used to in the loops ('while', 'for' etc). When executed, 'continue' statement will move the exection to the next iteration in the loop, without continuing the lines of code after the 'continue' inside the loop.

string []  names = new string[]{ "Little John", "Pete", "Jim", "Bill" };

foreach ( string name in names )
{
        if ( name == "Pete" )
             continue;

         Console.WriteLine ( name );
}

In the above sample, when the value of name is "Pete", it executes the 'continue' which will change the execution to the next iteration, without executing the lines below it. So, it will not print the name, if the name is "Pete".
The output of above sample would be :

Little John
Jim
Bill


switch

if you have to writeseveral if...else conditions in your code, switch statement is a better way of doing it. The following sample is self explanatory:

int i = 3;
switch ( i )
{
    case 5:
                 Console.WriteLine( "Value of i is : " + 5 );
                  break;
     case 6:
                  Console.WriteLine( "Value of i is : " + 6 );
                   break;
      case 3:
                   Console.WriteLine( "Value of i is : " + 3 );
                   break;
      case 4:
                    Console.WriteLine( "Value of i is : " + 4 );
                    break;
       default:
                    Console.WriteLine( "Value of i is : " + i );
                    break;
}

In the above sample, depending on the value of the conditional item, it executes appripriate case. In our code, since the value of i is 3, it executes the third case. The output will be as shown below:

Value of i is : 3

0 comments:

Post a Comment

Site Rate