C# tutorial for Beginners

C# pronounced as C-Sharp is a language developed by Microsoft that runs on the dotNET framework. C# is very similar to C and C++. If you already know one of these then you should be able to understand any C# script. In this tutorial, we will go into the basic building blocks of C# so that you get a good basic foundation.

C# was initially launched in 2002 and has gone through many variations since then. The current stable version is C# 10 and a new C# 11 version is in Beta. New versions of C# are generally released with Visual studio and dotNET framework updates.

Before we get started, you will need a code editor for testing your Code. It’s better to go with Visual studio code as it is both language and framework independent. If you decide to learn a new language at a later stage then you can still use the same code editor. You can also use an online code editor. Just go to Google and type in Online editor for C#, there are hundreds of them online.

Basic Building Blocks of C#

Namespace

You can think of namespaces like pre written libraries. They contain the codes that you can use inside your own code. For example, to print a message you need to type “WriteLine”. WriteLine takes in the argument you need to print on the screen. The code required to convert your message to a console window output is available in the system namespace and WriteLine is a method inside the system namespace.

You can add a namespace to your code with the “using” keyword.

using system;

Classes

Class in C# is a group of similar codes. It’s not necessary for you to have only related codes inside a class you can group the codes in any way you want. If you consider your codes as blocks then a class is a big container to hold your blocks.

You can give any name to your class. Just make sure it is not a keyword. For example, you cannot name your class as “class” or “using”.

To declare a new class just use the keyword class and then type in the name of your class. All the codes of a class are contained with {}

Unity sale

In order to reference a Class, you need to use an Object. We will go into objects in the later part of the tutorial.

class my_class
{
  
}

Datatypes

Datatypes are the classification of type of value for variables in programming. The following data types are the most commonly used

DatatypesDeclaration example
boolbool learning = true;
charchar ch = ‘a’;
floatfloat my_flaot=2.0f;
intint my_int=2;
stringstring my_name=”VionixStudio”;

Methods or Functions

Methods or Functions are the smaller containers within a class. A method contains small blocks of code that is executed when the method is called. A method can take arguments, return values.

A method can be set to private or public. If its set to private then you can call the method from within the class. A public method can be called using an object of the class.

You can also set a method as static to call it without an object reference but you still need to use the class name to call the method.

A Main function is required and its called when the class is referenced. You need to have a Main function in all classes in C#.

public void Main()
{

}

Operators and expressions

Expressions are a very common statement in programming. Most of the time you will find yourself comparing two values and assigning a value to a variable. The same operator can be used for different purposes. So it’s important to understand how to use them. For example, A=B is assigning the value of B to A but A==B is comparing A and B.

Arithmetic operations

OperatorExample
Addition “+”a+b
Subtraction “-“a-b
Multiplication “*”a*b
Division “/”a/b
Modulo “%”a%b

Comparison using operators

OperatorExample
Greater than “>”a>b
Less than “<“a<b
Equal to ==a==b
Not Equal !=a!=b
Conditional AND “&&”A&&B
Conditional OR “||”A||B

Not all operators support all datatypes. Float and integers can be used with > or < symbols. Similarly, you have to use Boolean for Conditional AND & OR.

Loops and Conditions

Loops are predefined codes used to do something in a repetitive manner. Depending on your requirement you can use a conditional block or a loop to execute your code.

Let’s try and understand the most commonly used loops and conditional statement.

If else Statement

It’s used to check a condition and execute a code. The else part is not compulsory so you can add it if you have a code that needs to be executed if the condition is false.

You can also have a If statement inside a if. This is called nested if and is very common in programming.

if(name=="VionixStudio")
{
     bool my_company=true;
}
else
{
     bool my_company=false;
}

For loop

For loop executes a code unlit the limit is reached. Say you want to increment a number by 1 till it reached 50 then you can use the for loop. Here is an example code

int i=0;
for (int num = 0; num < 51; num++) 
{
  i++;
}

While Loop

While loop is very similar to the if statement. The only difference is, it will keep on executing the code until the condition is false. Let’s use the same example in the for loop and try to do the same with while loop.

int i = 0;
while (i < 51) 
{
  i++;
}

Structure of a C# script

Now that you have the basic understanding of the basic building blocks of C# its time to see how the actual code looks like.

Here is the structure of a simple C# script

The namespace will be the first line of code in your script. Add all your required namespace on the top. Second is the class name. Inside the class declare the variables with the datatype that you are going to use. then you can create methods and loops inside the class.

Simple exercise to try

Go to Google and type “Onlin code editor for C#”.

Select any one you like

Use the namespace system and the WriteLine function to display your name.

Here is the sample code for you

using System;


class Program
  {
    static void Main()
    {
      Console.WriteLine("I am VionixStudio");    
    }
  }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.