System (using System namespace)
This page was last reviewed on Dec 22, 2023.
Dot Net Perls
System. This namespace contains many commonly-used types. These are separate from the language-level aliases in the C# language.
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.
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.
Finally This version adds the "using System" directive at the top. Now, the Console type can be accessed directly.
Tip Int32 can be accessed directly because it too is located in System. Many other common types require System.
// Version 1. class Program { static void Main() { System.Console.WriteLine(int.Parse("1") + 1); } }
// Version 2. class Program { static void Main() { System.Console.WriteLine(System.Int32.Parse("1") + 1); } }
// Version 3. using System; class Program { static void Main() { Console.WriteLine(Int32.Parse("1") + 1); } }
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.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 22, 2023 (edit).
© 2007-2024 Sam Allen.