![]() | ![]() |
Follow these steps to run your first C# program.
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:
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.