Use of using versus full name

Asked

Viewed 224 times

8

I’ve been following many projects open source and I realized that there’s a really big alternation between using using (Imports in VB.NET) and use direct reference to the namespace.

Example:

void Main()
{
    System.Int32 i = 10;
    System.Console.WriteLine(i);
}

or

using System;
void Main()
{
    Int32 i = 10;
    Console.WriteLine(i);
}

My question is: is there any convention when the direct reference or using/Imports? Or else, there is some significant difference between these two uses?

  • 1

    Look we usually use the whole reference because if there was System.Console.Writeline and Output.Console.Writeline and use is not known which Console to use.

3 answers

14


There is no clearly established criterion. It is more like.

Some people prefer to always use one way or the other. Others prefer to switch depending on what is being used or even more than how often it is used. If you are going to use a name only once, it is usually easier to write it whole in the place of use instead of making an "import". But there’s a downside to the consistency.

My personal observation is that it is rare to use a fully qualified name and there is a preference for using/import. The use of the full name is only adopted, in most cases, when there is a conflict of names.

It is still possible to apply the using creating an alias of its own in this way (credit for dcastro in the comments):

using WF = System.Windows.Forms.

Then you use the alias created (WF) to disambiguate the names.

C# 6 encourages the adoption of using a little more since now even static classes can be imported, or even static members of normal classes.

In other languages this may be different, but it seems you cared about C# and VB.NET secondarily. So ask yourself how many times have you seen someone using the first form.

More and more it will be common to use:

using System;
using static System.Console; //C# 6

void Main() {
    int i = 10; //raramente se usa o tipo do .NET, prefere-se o alias da linguagem
    //ou usa-se o var mesmo, neste caso
    WriteLine(i); //C# 6
}

I put in the Github for future reference.

  • Yeah, I asked the question already thinking about static using C# 6.0. I wanted to ask in general, but I have a preference for C# and VB.Net because of my experience. About using int or var instead of Int32 I’m aware I just used Int32 to reinforce the example. Anyway, thank you very much.

  • I would also add that if there are two guys with the same name in Scope, and if you only want to use one, you can get disambiguated with using SomeClass = Fully.Qualified.Name.SomeClass. Therefore, it is almost never necessary to refer to the type by its full name.

4

There is no standard but, generally, we use the direct reference because if there were System.Console.WriteLine and Output.Console.WriteLine and use using / import would not know which console to use thus generating a name conflict.

  • 1

    "we use the direct reference because if there were (...)" - Why use a solution to a problem that does not exist? Ambiguations are extremely rare, it makes no sense to use the full name often. Making it standard only contributes to reducing code readability.

  • The problem doesn’t exist yet, it may be that in c# (tag that AP added after all the answers) there is not so much but it has happened to me working with C++/Opencv/Tesseract

3

There is no convention.

Many will use the using, only by specifying the full name (with namespace) in case of name conflicts.

Surely there is no rule, no indication.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.