Basic Data Types and Methods in C#

In this post I will cover basic data types and methods in C# with examples.

Basic data types

char MyChar = ‘c’; // Just one character
String MyString = “hello there”; // Several characters
int MyInt = 15; // Integer
double MyDouble = 1.23; // Decimal
Boolean MyBool = true; // true or false

List

// A list of strings, containing the elements “apple” and “orange”
List ListOfStrings = new List(new String[] { “apple”, “orange” });

// Looping throught ListOfString (starting with index 0 and ending with index Length – 1) and printing all its elements
foreach (String s in ListOfStrings)
{
Console.WriteLine(s);
}

Dictionary

// A dictionary is a collection of key-value pairs. This dictionary has keys of type int
// and values of type string. Key 1 corresponds to value “potato”, and key 2 to “carrot”.

Dictionary MyDictionary = new Dictionary()
{
{ 1, “potato” },
{ 2 , “carrot” }
};

//This dictionary has both keys and values of type string. Key “first” has value “table”
// and key “second” has value “chair”

public static Dictionary MyStringDictionary = new Dictionary()
{
{ “first”, “table” },
{ “second” , “chair” }
};

// Printing all the values in the dictionnary MyDictionary
foreach (String item in MyDictionary.Values)
{
Console.WriteLine(item);
}

Basic Methods With Examples

If Else condition

int a = 1;
int b = 2;
int c = 3;
if( a == b )
{
Console.WriteLine("a equals b");
}
else if( a != c )
{
Console.WriteLine("a does not equal c");
}
else
{
Console.WriteLine("nothing above matched");
}

Do While loop

When using the do-while loop, the code will always go through the loop at least once. At the end, it will asses if it should re-enter or exit the loop. We should make sure not to create an endless loop. In this example, i starts at 0 and is augmented by 1 each loop. The loop will only be re-entered while i is less than 10.

int i = 0;
do

{
Console.WriteLine(i);
i++;
}
while (i < 10);

While loop

The while loop assesses its condition before entering to loop. Contrary to the do-while loop, depending on the condition, it is possible the while-loop is never entered.

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

For loop

In the for loop, the iterator and it’s conditions are given directly inside the parenthesis. In this example, the iterator i starts at 0 and augments by 1 each run. The loop will only be run as long as i is less than 10.

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

For each loop

The for-each loop is practical for going through a list of items. In this example we print all the elements in the list ListOfStrings, which is defined above.

foreach (String s in ListOfStrings)
{
Console.WriteLine(s);
}

Switch

Switch is used when we want to apply different behaviour depending on the value of the selector. It is important to add a break at the end of a case, otherwise the code will continue running the next case as well. Of course we can sometimes omit the break, if that is actually what we want to happen.

String select = "2";
switch (select)
{
case "1":
Console.WriteLine("case 1");
break;
case "2":
Console.WriteLine("case 2");
break;
case "3":
Console.WriteLine("case 3");
break;
default:
Console.WriteLine("none of the ebove matched");
break;
}

Try – Catch

Try-catch can be used to catch exceptions, in case we want to treat them ourselfs, or even if we just want to ignore them. We can catch all exceptions, or just specific exceptions, for example DivideByZeroException as shown in the commented code below.

int a = 0;
try
{
int divedyByZero = 10 / a;
}
catch(Exception ex) // Catch all exceptions
//catch(DivideByZeroException ex) // Catch specifically only DivideByZeroException
{
Console.WriteLine("Catched Exception: " + ex);
}
Console.WriteLine("Still running OK"); // Without try-catch would have stopped run on error (divide by zero)

Leave a Reply

Your email address will not be published. Required fields are marked *