October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The C# Data Types and Programming Constructs

  • September 19, 2002
  • By Anand Narayanaswamy
  • Send Email »
  • More Articles »

Boxing and Unboxing

The conversion of a value type into a reference type is termed as boxing; the conversion of a reference type into a value type is termed as unboxing. Verify the code in Listing 3:

Listing 3

// Value type
int X = 100;

// x boxed to a object type which is reference based,
// no explicit cast required
object Y = X; 

// y unboxed to an integer type, explicit cast required
// while unboxing
int Z = (int)Y

Programming Constructs

In this session, we will discuss the commonly used programming constructs like if-else, Switch-case, for, while, and do-while loops. Let's analyze each of them in detail.

If - Else

This is one of the popular decision-making statements, which is similar to that of its C++ and Java counterparts. Its usage is given below.

Usage

if(Condition)
{
  Statements
}
else
{
  Statements
}

If the condition satisfies, the statements inside the if block will be executed; otherwise, statements inside the else part will execute as shown in Listing 4:

Listing 4

using System;
class Fund
{
  public static void Main()
  {
    int x1 = 50;
    int x2 = 100;
    if(x1>x2)
    {
      Console.WriteLine("X1 is greater");
    {
    else
    {
      Console.WriteLine("X2 is greater") 
    }
  }
}

Switch - Case

This is also one of the decision-making statements which is regarded as an alternative to if - else. Its usage is given below.

Usage

switch(expression)
{
  case 10: Number is 10;break
  case 20: Number is 20;break
  case else: Illegal Entry;break
}

When the expression matches with a case value, the corresponding statements would be printed. Listing 5 explains the usage of Switch - Case statement.

Listing 5

using System;
class Switchexample
{
  public static void Main()
  {
    int wday = 2;
    switch(wday)
    {
      case 1: Console.WriteLine("Monday");break;
      case 2: Console.WriteLine("Tuesday");break;
    }
  }
}

Looping Statements

For loop

This loop is used to iterate a value fixed number of times. Its usage is given below:

Usage

for(Initial Value, Condition, Incrementation / Decrementation)
{
  Statements
}

You have to specify Incrementation and Decrementation using ++ and - operators. Listing 6 examines the working of the for loop. Here, numbers from 1 to 10 will be printed as output. 10 will also be printed because we have used <= operator. Try using only the +< operator and observe the result.

Listing 6

using System;
class Forexample
{
  public static void Main()
  {
    for(int I = 1; I<=10;I++)
    {
      Console.WriteLine(I);
    }
  }
}

While loop

This loop is similar to the for loop, except that condition is specified first and is used to repeat a statement as long as a particular condition is true. The usage of while loop is given below:

while(Condition)
{
  Statements
}

If the condition is false, statements within the loop will not be executed. Listing 7 examines the working of while loop:

Listing 7

using System;
class Whileexample
{
  public static void Main()
  {
    int I = 1;
    while(I <=10)
    {
      console.WriteLine(I);
    }
  }
}

Do - While

This looping statement is also similar to that of the previous one but the condition is specified last. Its usage is given below:

Usage

do
{
  Statements
}while(Condition)

In this case, the condition is tested at the end of the loop. Hence, the statements within the loop are executed at least once, even if the condition is false. Let's verify an example:

Listing 8

using System;
class Whileexample
{
  public static void Main()
  {
    do
    {
      int I = 1; 
    } while(I<=10);

    Console.WriteLine(I);
  }
}

Foreach

If you had used Visual Basic, you would be familiar with the foreach statement. C# also supports this statement. Listing 9 illustrates the usage of this statement:

Listing 9

using System;
using System.Collections;
class Foreachdemo   
{
  public ArrayList numbers;

  Foreachdemo() {
  numbers = new ArrayList();
  numbers.Add("One");
  numbers.Add("Two");
  numbers.Add("Three");
  numbers.Add("Four");
  numbers.Add("Five");
}

public static void Main() 
{
  Foreachdemo fed = new Foreachdemo();

  foreach(string num in fed.numbers) 
  {
    Console.WriteLine("{0}",num);
  }
}

In the above example, an object of type ArrayList is created and values are added to it using the Add method of the ArrayList class. Try removing the second using directive and observe the result.

Break statement

This statement is used to terminate a loop abruptly. We have seen the usage of this statement while discussing decision-making statements. Listing 10 examines the working of this statement.

Listing 10

using System;
class Whileexample
{
  public static void Main()
  {
    for(int I = 1; I<=10; I++)
    {
      Console.WriteLine(I);
      if(I==5)
      {
        break;
      }
    }
  }
}

About the Author

Anand Narayanaswamy works as a freelance Web/Software developer and technical writer. He runs and maintains learnxpress.com, and provides free technical support to users. His areas of interest include Web development, software development using Visual Basic, and in the design and preparation of courseware, technical articles, and tutorials. He can be reached at anand@learnxpress.com.

# # #





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel