C# Basic

C# is a  programming language. Like other programming languages, C# has rules, manners that you need to follow when you write codes.

C# is objective programming language.  Which means you can create object and use them with other object to manage behavior within objects. This may sounds bit complex but you will understand the beautify of objective programming languages as you go along. So at this point, don’t worry about what it means so much.

C# statement looks like this

This declare integer type variable called MyNumber and inserting value 10 to it.

and variables comes with methods like


You notice that at the end of statement, I use “;”.  This means it is the end of statement.


If I were to explain what is programming in one line, programming is doing things with stored data and generating output.  And variable is a place to store data.

In C# you have to declare types.  Some languages you don’t have to declare types, let you have dynamic types of variables.  They both have pros and cons.  In C# types are required because there are special methods, fields and events attached to each types of variables.

The basic of variables are;

You can also refer to https://msdn.microsoft.com/en-us/library/ms173104.aspx for more about how variable works in C#.

Conditions and Execution

One of the most common thing you do in coding is, execute different actions depending on values, using if or switch etc.


If statement is like below

and you want to use else statement if the boolean value is not true.

You can have multiple if condition statement by using else if statement


switch is statement that comes in handy when you want to have many different logic depends on value passed.

code above; I declare integer type called i which is set to 1 first.  Then, I have switch statement to do something depends on the value of i.

If the i = 1, which I say case 1: then set i to 2.  And if i does not fit in any of cases that I define, then set i to 0.

Don’t forget to “break” it, this is saying, I want to break out from the switch statement and proceed.

You should always have default: in case the value you pass does not fit any cases.

For more, check https://msdn.microsoft.com/en-us/library/06tc147t.aspx

Looping through

Another popular things that you do in your code is looping through.

There is for loop statement and while loop statement.  For loop basically tells to loop specific number of times, like below.

The format is;
for (initial setting, condition statement, dynamic condition)

when you need to loop through array, my recommendation is to write like below;

or you can also use while statement to loop while the condition you specify is true.

As a rule of thumbs, I recommend you use while if you don’t know or not sure how many times you need to loop, like reading text file to the end.

If you want to run one loop of execution unconditionally, you can do

If you want to break out of the loop, you can,

If you want to continue on with looping,

This code will show, 0 and 2 (skipping 1).


Leave a Reply