TheDeveloperBlog.com


C# Using System

System contains many commonly-used types. These are separate from the language-level aliases in the C# language. System can be referenced with different syntax forms. It is usually specified with a using System directive.


Example. We look at three different representations of the same program. The first version uses the System.Console type—it omits the "using System" directive. It also uses the int alias in the C# language.

ConsoleInt
Version 1: C#

class Program
{
    static void Main()
    {
	System.Console.WriteLine(int.Parse("1") + 1);
    }
}

Version 2: C#

class Program
{
    static void Main()
    {
	System.Console.WriteLine(System.Int32.Parse("1") + 1);
    }
}

Version 3: C#

using System;

class Program
{
    static void Main()
    {
	Console.WriteLine(Int32.Parse("1") + 1);
    }
}

Output

2

The second version changes the 'int' alias to the 'System.Int32' type found in the System alias. The C# language automatically does this. It is typically best to use the 'int' keyword for readability.

The third version adds the 'using System' directive at the top. Now, the Console type can be accessed directly. Also, Int32 can be accessed directly because it too is located in System.

Tip: You can fix several common errors by adding the System namespace to your program.

And: If there are any calls to Console.WriteLine, the System namespace is required. Many other common functions require System as well.


Discussion. The C# language uses aliased keywords that map to types. For example, if you use the keyword int, you are actually using System.Int32. However, because "int" is at the language level, you do not need to include System to use it.

Further, there are many namespaces in the .NET Framework, such as System.IO, that are sub-namespaces to System. These are not automatically included with System. You must include System.IO to get the input-output namespace as well.

Tip: Another root namespace available in Visual Studio is the Microsoft namespace.

And: This contains some useful types, such as those required to do Microsoft Office interoperation.

Excel

Summary. System can be understood as a commonly-used subset of the types in the .NET Framework. It is not part of the C# language exactly, but rather it includes types that are mapped to by the aliased keywords in the C# language such as 'int'.