Use of using versus full name

8

I've been following many open source projects and noticed that there is a fairly large switch between using using ( Imports in VB.NET) and using direct reference to 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 a convention of when to use direct reference or using / Imports ? Or is there a significant difference between these two uses?

    
asked by anonymous 17.07.2015 / 19:04

3 answers

14

There is no clear-cut criterion. It's more like it.

Some people always prefer to use one way or another. Others prefer to switch depending on what is being used or even more how often it is used. If you're going to use a name only once, it's usually easier to write it in the place of use instead of doing an "import." But it has the drawback that takes away the consistency.

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

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

using WF = System.Windows.Forms.

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

C # 6 should encourage the adoption of using a little more since now static classes can be imported.

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

Increasingly, 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
}
    
17.07.2015 / 19:19
4

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

    
17.07.2015 / 19:17
3

There is no convention.

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

Surely there is no rule or indication.

    
17.07.2015 / 19:15