C# System (using System namespace)

Understand the System namespace and when it is needed. See example of 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.
Notes, System. System is usually specified with a using System directive. It is included in a default C# file in Visual Studio, but it is worth understanding why it is there.Namespace
An example. We look at 3 representations of the same program. The programs have the same output, and should compile to the same intermediate representation.

Version 1: The first uses the System.Console type—it omits the "using System" directive. It also uses the int alias in the C# language.

Int, uint

Version 2: This version changes the int alias to the System.Int32 type found in the System alias. The C# language automatically does this.

Version 3: This 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. Many other common types require System.

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
Notes, 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.
Discussion. The C# language uses aliased keywords that map to types. For example, if you use the keyword int, you are using System.Int32.

However: Because "int" is at the language level, you do not need to include System to use it.

Discussion, continued. 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.

Tip: You must include System.IO to get the input-output namespace as well. This can fix compile-time errors.

Microsoft: 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.

A summary. System can be understood as a commonly-used subset of framework types. It includes types that are mapped to by the aliased keywords in the C# language such as int.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls