22

I recently stopped using s and instead use the full namespace path of any object that I call.

Example:

using System;    

namespace QuizViewer
{
    class Class1
    {
        Console.WriteLine("Hello World!");
    }
}

This is what I do now.

namespace QuizViewer
{
    class Class1
    {
        System.Console.WriteLine("Hello World!");
    }
}

Before you ask why I do this, I am using this style so that I can see exactly where my objects are coming from and it's easier when using the different Timer objects and other objects with similar names.

Is there any performance increase or decrease in this style of programming?

Nae
  • 14,209
  • 7
  • 52
  • 79
Kyle Uithoven
  • 2,414
  • 5
  • 30
  • 43
  • 11
    I'd say that this style will lead to a significant decrease in the performance of the _person_ who is reading or writing the code... (It's fair enough to do this for a few classes, such as `Timer`, where there are several equally-named classes, but for the most part, I'd consider the namespaces to be noise.) – Aasmund Eldhuset Jul 08 '11 at 18:10
  • You can always hover over a type name in Visual Studio to see the full Namespace and Classname of the type. – Kyle Trauberman Jul 08 '11 at 18:12
  • 4
    Note that you are talking about *using directives*, not *using statements*. The *using statement* is the form `using(var stream = File.Open(...)) { ... }`. – Eric Lippert Jul 08 '11 at 18:38
  • Note also that this related question might help you understand why there is no performance impact of this change: http://stackoverflow.com/questions/6614375/c-why-code-compiles-without-any-name-spaces-included/6614804#6614804 – Eric Lippert Jul 08 '11 at 18:39
  • 7
    Finally, note that if you are eschewing "using" because of a specific confusion between two similarly-named things, you can use an *using alias directive*: `using FrobTimer = BogoSoft.Froboznicator.Timer;` -- now you can use the identifier `FrobTimer` in that file and the compiler will know that you mean the fully-qualified type. – Eric Lippert Jul 08 '11 at 18:41
  • Readabilty will suffer... – Mariusz Jamro Apr 08 '16 at 06:16

6 Answers6

41

There is zero performance difference because the compiler ALWAYS puts in the full name - using is only a hint for the compiler, the runtime doesn't know or support that.

However, once you memorize where the objects come from you will look at this as silly and verbose. There is just so much noise and people just know that Path is from System.IO, Console is in System and StringBuilder is in System.Text.

One downside of your approach: Without using, no extension methods outside of the current namespace. Have fun writing System.Linq.Enumerable.Where(inputSequence,...) instead of just inputSequence.Where(...) :)

Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • For one `Where()`, it's still bearable. But a more complicated query would quickly become illegible. – svick Jul 09 '11 at 12:57
6

Short answer no: it is the same code that is compiled.

Avada Kedavra
  • 8,523
  • 5
  • 32
  • 48
6

I think that this style result in a programmer performance decrease :). I use the using statement and usually it is clear from code to which namespace the class belong. If not, press F12.
Just my 2c.

platon
  • 5,310
  • 1
  • 22
  • 24
  • In the case of this, how do you "tell" which timer it is before pressing F12? System.Timers.Timer _timer; System.Threading.Timer _timer2; System.Windows.Forms.Timer _timer3; – Kyle Uithoven Jul 08 '11 at 18:12
  • 2
    Hover over the `Timer` type name or an instance of it to see the full type name. – Kyle Trauberman Jul 08 '11 at 18:15
  • VS is really good about warning you when a conflict occurs, and forces you to use the full namespace in your code when that occurs. – Kyle Trauberman Jul 08 '11 at 18:16
  • hmmm, if you are using simple _Timer timer;_ declaration(sorry for multiple code in comments), then the code will only compile with the single using declaration. I.e. the using will have to either _using System.Windows.Forms;_ or _using System.Threading;_ or _using System.Timers;_ Also, due to specific of my work, I saw many (really many) code written by not me. It was not a problem. When I used Delphi, I wrote _with_, now - _using_. No problems. Finally, intellisense and F12 help if i do not know from which namespace the class comes. – platon Jul 08 '11 at 18:19
  • 3
    Just want to continue ... From my point of view, using _var_ in C# everywhere instead of declaring a strong typed variable is really a bad style code... – platon Jul 08 '11 at 18:20
  • 3
    @platon using var in C# does not stop a variable being strongly typed. From [MSDN](http://msdn.microsoft.com/en-us/library/bb384061.aspx) - "It is important to understand that the var keyword does not mean "variant" and does not indicate that the variable is loosely typed, or late-bound. It just means that the compiler determines and assigns the most appropriate type." – luketorjussen Jul 08 '11 at 19:03
  • 1
    Indeed, I agree, I may be expressed myself badly. I do not like to see code where all the variables are declared as var. It is a nightmare for a programmer who did not write this code. It is awful to read the code written this way and I always have an idea to change it ... :-) – platon Jul 08 '11 at 19:06
5

There's no performance impact; it's mostly a stylistic choice. I find that using using statements reduces the clutter. Plus, with Intellisense, it's easy enough to see the fully qualified namespace.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
5

The only performance hit, is the hit you take to type it all out, and with you or others reading it.

Using statements are to help readability, not really for performance.

Rangoric
  • 2,739
  • 1
  • 18
  • 18
1

If you disassemble both of this pieces of code and look at IL code, you'll find that compiler always references all the types by it's full names. That is absolutely identical ways to operating types.

Aave
  • 548
  • 1
  • 5
  • 15