First C# program

This post will show you how to write your first C# program. Please make sure that you have installed Visual Studio on your machine. If you have not yet installed VS, download and install Visual Studio first.

First C# program

This post will show you how to write your first C# program. Please make sure that you have installed Visual Studio on your machine. If you have not yet installed VS, download and install Visual Studio first.

Downloads | IDE, Code, & Team Foundation Server | Visual Studio
Download Visual Studio Community, Professional, and Enterprise. Try Visual Studio Code or Team Foundation Server for free today.

Creating first C# program

  • Open Visual Studio and create a Console Application.
  • Modify the main() method as shown below.
using System;   //Adding .NET namespaces
namespace LearnCSharp   //Namespace of the class
{
    class Program   //The class
    {
        static void Main(string[] args) //Main method
        {
            Console.WriteLine("Hello World");
            Console.ReadKey();    //This keeps the console alive
        }
    }
}

Output

Hello world

About the program

  • using  System - The using keyword is used to include namespace (collection of classes) to the program.
  • namespace LearnCSharp - Namespace of the current class is declared using the namespace keyword.
  • class Program - A class named Program is declared. A class is declared using the class keyword.
  • static void Main(string[] args) - Here, we define the main() method. It is the entry point of a console application.
  • Console.WriteLine() - WriteLine() is a method of the Console class. These classes and methods are defined in the System namespace. The WriteLine() method is used to display text on the console.
  • Console.ReadKey() - This code makes the console application to wait for a key-press, before closing the console.

Share Tweet Send
Loading...