C#: using System

In our earlier example, there was a couple of using statements created by the default C# template in MS Visual Studio.

Using in C# is called a directive.

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

As the program needs only to print out a simple Hello, World line, we can simplify the program by using only using System.

So the code below will also work:

using System;

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

The unused Usings can also be removed by right clicking on the page.  Look for Organized Usings -> Removed Unused Usings.

usings

System is a namespace in C# and actually, we have defined our program with a namespace of ConsoleHelloApplication.

With the line using System, we can use the Console class to write out the line under the System namespace.

The System namespace contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions.

If we do not include the System namespace, the code will be as below where System.Console is explicitly written out.

namespace ConsoleHelloApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello, World");
            System.Console.ReadLine();
        }
    }
}

 

Series Navigation<< C#: Print a line using WriteLine
C#: ASCII and Unicode >>