C# Using System

.NET Framework information

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.

This C# article describes the System namespace. It provides an example of System.

System

Here we look at three different representations of the same program. The first version uses the System.Console type but does not add the 'using System' directive. It also uses the int alias in the C# language.

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
Int keyword

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

Third version. 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.

Language keywords (aliases)

The C# programming language

The C# language uses aliased keywords that map to types in the System namespace. 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.

Common errors

You can fix several common errors by adding the System namespace to your program. If there are any calls to Console.WriteLine, the System namespace is required. Many other common functions require System as well. To fix these, simply add System to the using directives.

Sub-namespaces

Programming tip

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

Other root namespaces

Another root namespace available in Visual Studio is the Microsoft namespace. This contains some useful types, such as those required to do Microsoft Office interoperation.

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'.

.NET Framework Info
.NET