joseville
joseville

Reputation: 953

Why is the `using System` directive not necessary here? C# | Visual Studio Code

The C# doc on the using directive says

The using directive allows you to use types defined in a namespace without specifying the fully qualified namespace of that type.

I understood this to mean that if you want to use System.Console, you have two options -- either

  1. include a using System directive in your code which allows you to refer to System.Console simply as Console; or,
  2. fully-qualify System.Console -- i.e. refer to System.Console as System.Console.

Yet, I have the following C# code in Visual Studio Code and it works even though I didn't include a using System directive and even though I refer to System.Console as just Console

Console.WriteLine("Hello World!");
List<String> names = ["Alice", "Bob", "Eve"];
foreach (String name in names) {
    Console.WriteLine($"Hello {name}!");
}

I am neither fully-qualifying System.Console nor did I include a using System directive. Similarly for List. Yet, the code runs fine and prints the following the console:

Hello World!
Hello Alice!
Hello Bob!
Hello Eve!

Upvotes: 1

Views: 46

Answers (0)

Related Questions