0

I want to use Console.WriteLine method. There are two ways

  1. using System; and then Console.WriteLine(); this is one way

  2. System.Console.WriteLine(). this is another way

What is the difference between these two?

Will all the classes include under system namespace if i use using System; ?

Will console class only include when i use System.Console.WriteLine statement?

crthompson
  • 15,653
  • 6
  • 58
  • 80
  • the main difference is reduced keystrokes...then there is the question about Namespace Aliases that help resolve ambyguous names...even without the using all the classes "are there" because the reference to the dll,without the using u need the fully qualyfied name. – terrybozzio Jul 05 '14 at 17:03

2 Answers2

2

using System is just a syntactic sugar that allows you to access all the classes under the namespace without specifying the namespace. So yes, using System imports all the classes that are defined in System namespace then you can access them by their name.

In case where you include two namespaces that has two identical class then you will have to use fully-qualified name of the class to avoid ambiguity.

Also it is worth noting that this is just a shortcut. The using directive doesn't add anything to your project.So if you need to use a class or a function from a third-party dll, first you have to add a reference to your assembly (*.dll file) then you can use using directive.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Very good answer - just to add that obviusly the main advantage of 'USING' would be that your calls would be shorter. – AltF4_ Jul 05 '14 at 17:05
0
using System;

<CODEHERE>
<CODEHERE>
    Console.WriteLine("Text");
<CODEHERE>
<CODEHERE>

This is the more generic, and usual way of doing it for two reasons:

  1. It is more readable
  2. And if you wanted to use another class of System, you would already have the import.

Using the other method:

System.Console.WriteLine("Text");

is a lot more rare to see, and you would have to write out the fully qualified name of any other System class, or just to use it again:

System.Console.WriteLine("Text");
System.Console.WriteLine("Text2");

If you look back at what the using statement actualy means:

'Using' is a statement used to stop the programmer from repeatedly having to type the fully qualified namespace for everything.

I would use the 'using System' version.

Steve
  • 1
  • 2