image
image
image

Running Your First Program

image

Follow these steps to run your first C# program.

  1. Open Visual Studio. You can do so by searching “Visual Studio” at the windows search box as shown below:

image

  1. From the top menu, select File -> New -> Project. Look at the following screenshot for reference.

image

  1. You will see a list of different types of programs that you can create with C#.  Select “Console App (.NET Framework)”, give any name to your application, I named it “MyProgram”. Click OK button.

image

  1. You will see a window where you can write C# code. Write the following code in the window and press the green triangle with label “Start”, from the top menu. I will explain what is happening later.

image

Once you execute the script above, you will see that a console window, like the command line, will appear that will contain the message “Congratulations, you wrote your first Program” as shown below:

image

Now let’s see what is actually happening. Take a look at the code that we wrote:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace MyProgram

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Congratulations, you wrote your first Program");

Console.ReadKey();

}

}

}

The using statements at the top of the code are used to import any libraries. Libraries are basically set of pre-built functions that we can directly use in our code. Every code snippet in C# has to be inside a class which in turn lies inside a namespace. In the script above our class name is Program and it lies inside the MyProgram namespace.

Inside the Program class, we have method Main whose return type is void and method type is static. This is the starting point of our code. Do not worry much about the terms void and static at the moment. We will see these terms in detail in a later chapter. For now, just remember that the Main method is the starting point of C# code.

Next, we write a simple line on the screen. To do so, we used WriteLine method of the Console  class and passed it the text that we wanted to display on the console window. Finally, we used the ReadKey method of the Console class so that our console window doesn’t disappear immediately after printing the text. And that’s pretty much it. Congratulations on successfully executing your first program.