CSharp > Step by Step - Part1

Run first C# Program in CSharp

How to run your first C# Program, let's understand this.


When Visual studio created the project it generated some basic C# code in ‘program.cs’ file , let’s try to understand this basic code.

‘program.cs’ first starts with a bunch of “using” statements. ‘using’ statements indicate which components from .NET framework are used in the program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

The next thing in the code is “static void main” , this thing is termed as function.

static void Main(string[] args)
{
Console.WriteLine("Hello");
}

A function is a bunch of C# statements to which you can pass data and get output. If you watch closely “main” function is having an input argument “args” and returns nothing for now. This nothing is shown by the keyword “void”.

You can also see “static” keyword for now just keep it on hold we will discuss about this keyword in depth later.

Inside the “main” function we have written a single line of code: - “Console.WriteLine(‘Hello’)”. This code displays the “Hello” content on the computer monitor. In this single line of code “Console” is the namespace and “WriteLine” is a function and we passing “Hello” content to be displayed.

So we have encountered a new word termed as “namespace” lets discuss more about it.

Code is actually written in a function, function is grouped in to classes and classes belong to namespaces. In simple words Namespaces are logical grouping of classes.

We will discuss about classes and namespaces in OOP chapter later.

If you see the complete code of “program.cs” you would get the idea of the logical grouping which we have explained in the previous section.

namespace Lab1LearnCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello");
        }    
    }
}

Below is the complete code of “program.cs” with the using keyword.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab1LearnCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello");
        }
    }
}

In order to run the program press CONTROL + F5 button. This would build the program as well as run it.

 Views: 12373 | Post Order: 9


Write for us






Hosting Recommendations