Understanding Data Types and Variables in C#

Data Types are an integral part in any programming language. It represents how to express numbers, characters, strings, decimal point values etc. To illustrate, if you want to express 25 in C#, you can write a code as shown below:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">int x = 25

In the above code, int represents the integer data type. x denotes a variable name. Variables are nothing but a storage area in the memory. 25 is the value for the variable x. Without a variable you cannot store values. Keep in mind that int is an alias for the data type Integer. It is having a fixed limit beyond which the data type will not hold values.

Like Integer, all data types are having aliases and a fixed range of values. You should use a data type best suited to your programming needs. For example, if you are writing code for developing a calculator, it’s better to use data types, which will accept large values. This will avoid runtime program errors.

Let’s look at another scenario. The data type Byte accepts values only up to 255. If you try to apply a value of 300 for the corresponding variable, there will be errors during the execution of the program. These errors are called as Exceptions, which will be discussed in an another article. If you had applied the data type Integer for the above program, this problem would have been avoided. Please go through Table 1 for a detailed list of all data types.

Table 1

System.Byte –> byte

System.Sbyte –> sbyte

System.Int16 –> short

System.Int32 –> int

System.Int64 –> long

System.UInt16 –> ushort

System.UInt32 –> uint

System.UInt64 –> ulong

System.Single –> float

System.Double –> double

System.Object –> object

System.Char –> char

System.String –> string

System.Decimal –> decimal

System.Boolean –> boolean

Basically, data types in C# are classified into two, viz; Value based and reference based types. Let’s examine each of these types in detail. Value Based Data Types include all numerical data types like Integer, Float, Byte and etc. Here you are working directly with values. When you try to assign one value type to another, a bit wise copy is achieved as shown in the listing given below:

using System; 
struct Valdata  
{ 
public int a; 
public int b; 
} 
class Valtype&#160; 
{ 
public static void Main(string[] args)  
{   
Valdata v = new Valdata(); 
v.a = 200; 
v.b = 300; 
Valdata v1 = v; 
Console.WriteLine(v.a); 
Console.WriteLine(v.b); 
Console.WriteLine(v1.a); 
Console.WriteLine(v1.b);   
Console.WriteLine(&quot;##################################&quot;); 
v1.a = 800; 
Console.WriteLine(v1.a); 
Console.WriteLine(v.a); 
} 
} 

Note that even if you change the values of one variable, the value of another variable won’t change. Reference types include Classes and Interfaces. Here, if you change the value of one variable the value of the other variable also reflects the same value. Also, after creating an object of a class, you are working directly on that object and not with the actual values as in value types. Hence copies of reference types results in a shallow copy. To illustrate, change the definition of the above type from a C# structure to a C# class (in the above code) and observe the result.

READ  What do you mean by .NET Framework Class Libraries?
Boxing and Unboxing

The conversion of a value type into a reference type is termed as boxing while the conversion of a reference type into value type is termed as unboxing. Verify the code given below:

// Value type

<i>int x</i><i> </i><i>= </i><i>100</i><i>; </i><i></i>

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

<i>object Y = X;   </i> 

// y unboxed to an integer type, explicit cast required while unboxing

<i>int Z = (int)Y </i>
&#160;

Programming Constructs

In this session, we will discuss about the commonly used programming constructs like if-else, Switch-case, for, while, do-while loops. Let’s analyze each one 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) <pre>{ 
Statements 
} 
else&#160; 
{ 
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&gt;x2) 
{ 
Console.WriteLine(“X1 is greater”); 
else 
Console.WriteLine(“X2 is greater”)  
} 
} 
} 

Switch – Case

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

Usage

switch(expression)&#160; 
{ 
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;

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">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

READ  Fix: A project with an Output type of Class Library cannot be started directly

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

Usage

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">for(Initial Value, Condition, Incrementation / Decrementation) 
{ 
  Statements
} 

You have to specify Incrementation and Decrementation using ++ and – operators. Listing 6 examines the working of 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 < operator and observe the result.

Listing 6

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
class Forexample { 
public static void Main() { 
for(int I = 1; I<span style="color: #0000ff">&lt;</span>=10;I++) { 
Console.WriteLine(I); 
} 
} 

While loop

This loop is similar to 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:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">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

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
class Whileexample 
{ 
public static void Main() 
{ 
int I = 1; 
while(I <span style="color: #0000ff">&lt;</span>=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:

READ  Understanding Structures and Enumeration using C#

Listing 8

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 

class Whileexample 

{ 

public static void Main() 

{ 

do 

{ 

int I = 1; 

} 

while(I<span style="color: #0000ff">&lt;</span>=10); 

Console.WriteLine(I); 

} 

} 

  • Foreach

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

Listing 9

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 

using System.Collections; 

class Foreachdemo { 

public ArrayList numbers; 

Foreachdemo() { 

numbers = new ArrayList(); 

numbers.Add(&quot;One&quot;); 

numbers.Add(&quot;Two&quot;); 

numbers.Add(&quot;Three&quot;); 

numbers.Add(&quot;Four&quot;); 

numbers.Add(&quot;Five&quot;); 

} 

public static void Main() { 

Foreachdemo fed = new Foreachdemo(); 

foreach(string num in fed.numbers) { 

Console.WriteLine(&quot;{0}&quot;,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

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 

class Whileexample 

{ 

public static void Main() 

{ 

for(int I = 1; I<span style="color: #0000ff">&lt;</span>=10; I++) 

{ 

Console.WriteLine(I); 

if(I==5) 

{ 

break; 

} 

} 

} 

One Response to "Understanding Data Types and Variables in C#"

  1. data types   October 25, 2012 at 12:08 am

    The grid provides broad capabilities of working with data such as arbitrary class objects, row arrays, objects implementing IList or IDictionary interfaces and objects with variable number of fields

    Reply

Leave a Reply

Your email address will not be published.