In C # can I use a local alias for class or namespace?

6

In languages such as Python or PHP you can use a local alias for classes or namespaces .

PHP example:

 use Vendor\Package\ClassName as C;
 use Vendor\Package as P;

 $class = new C;

 $class = new P\ClassName;

Python example:

from json import dump as d

d({"name": "value"})

I call the operations above "local alias" because only in that script is that class or namespace will have the alias defined. That is, it is not accessible in other scripts .

Is it possible to define a local alias in C #, as it does in PHP and Python?

    
asked by anonymous 14.06.2016 / 17:51

4 answers

6

You can use various types of aliases in C #. You can use a simple one:

using WF = System.Windows.Forms; //só fez o alias do namespace

Usage:

var botao = new WF.Button();

This can be most useful when there is ambiguity. Because deep down when there is not, that WF would not even be necessary. So why create an alias to use in a place that would not even have to have anything? Some even like to be more explicit to avoid reading ambiguity. As there may be future ambiguity, the programmer already puts the full name there. As these names can be very long this way facilitates a little typing and reading space.

A better example might be in a place that needs to use a specific Stream and in the same code use another that has the same elements (same types) with the same names. If you do not put the complete qualifier, the compiler refuses for obvious reasons. Putting the full qualifier is too long, you create a shorter alias.

The ideal is to use caution, you can not play with names by taste, it has to be a way to solve one problem, not cause another.

Or you can do something more complex:

using Dict = System.Collections.Generic.Dictionary<string, List<string>>;

Usage:

var dict = new Dict { "palavra", new List<string> { "definição1", "definicão2" },  "palavra2", new List<string> { "definição1", "definicão2" } };

Here it is more interesting because the alias is not of the namespace but of the type, and a complex good that would be annoying to use several times in the code, in this way can refer to it in a simpler way without adding anything in the code, as with WF above.

Some will say that this is more DRY , I disagree that this is exactly DRY, it just avoids repetition.

Another legal example of Marc Gravell's response .

namespace RealCode {
    //using Foo; // can't use this - it breaks DoSomething
    using Handy = Foo.Handy;
    using Bar;
    static class Program {
        static void Main() {
            Handy h = new Handy(); // prove available
            string test = "abc";            
            test.DoSomething(); // prove available
        }
    }
}
namespace Foo {
    static class TypeOne {
        public static void DoSomething(this string value) { }
    }
    class Handy {}
}
namespace Bar {
    static class TypeTwo {
        public static void DoSomething(this string value) { }
    }
}

Documentation .

    
14.06.2016 / 17:59
4

Yes, you can set alias for namespaces and classes by directive using .

using Foo = System.Globalization.CultureInfo; // Alias para a classe 'CultureInfo'
using Bar = System.Globalization; // Alias para a namespace 'System.Globalization'
    
14.06.2016 / 17:58
3

In C# you can set a NameSpace alias , such as can be seen in the documentation.

To use, just put using nomeAlias = NameSpace.Completo .

An example of its use would be:

using teste = System;

namespace ConsoleApplication1
{
  public class Program
  {
    public static void Main(string[] args)
    {
      teste.Console.WriteLine("teste Alias");
    }
  }
}

Where teste is an alias for System .

See the example in DotNetFiddle .

And when should I use?

There is no "rule" for use, but it is used when there is a conflict of NameSpaces (ambiguity) or when NameSpace is hidden in another entity.

    
14.06.2016 / 17:58
1

Yes.

Type the nickname ( Linq ) and match namespace ( System.Linq ) to create the nickname

using Linq = System.Linq;

Encoding:

Linq.IQueryable<>
Linq.Enumerable

Links:

14.06.2016 / 17:54