Search code examples
c#using

What does "using System" mean in C#?


What does

using System;

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

["using System"] mean? Why can't you start your code without these lines?


Solution

  • The using System line means that you are using the System library in your project. Which gives you some useful classes like Console or functions/methods like WriteLine.

    The namespace ProjectName is something that identifies and encapsulates your code within that namespace. It's like packages in Java. This is handy for organizing your codes.

    class Program is the name of your entry-point class. Unlike Java, which requires you to name it Main, you can name it whatever in C#.

    And the static void Main(string[] args) is the entry-point method of your program. This method gets called before anything in your program.

    And you can actually write a program without some of them in DotNet 5 and later as they now support top-level functions.