If you use "using System; why do you need to use another line that says using System.anything?
1
Expert's answer
2012-09-27T11:30:16-0400
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. 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
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.
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. If there are any calls to Console.WriteLine, the System namespace is required. Many other common functions require System as well.
Comments
Leave a comment