In C# is it possible to use a local alias for class or namespace?

Asked

Viewed 492 times

7

In languages like Python or PHP it is possible to 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 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.

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

4 answers

8


It is possible to use several types of aliases in C#. You can use a simple:

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

Use:

var botao = new WF.Button();

This can be more useful when there is ambiguity. Because deep down when there is, this WF nor would be necessary. So why create an alias to use in a place that would not need to have anything?

Some even like to be more explicit to avoid reading ambiguity, but being explicit can leave the code long. As there may be future ambiguity, the programmer already puts the full name there. As these names can be very long this way makes typing and reading space a little easier.

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

The ideal is to use with caution, can not keep playing with the names by taste, has to be a way to solve a problem, not cause another.

Or you can do something more complex:

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

Use:

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

I put in the Github for future reference.

Here is more interesting because the alias is not from namespace and yes of the type, and a very complex that would be boring to use several times in the code, this way can refer to it more simply without adding anything in the code, as happened with the WF above.

Some will say that this is more like DRY, I disagree that this is exactly DRY, just avoid repetition.

Another legal example of Reply by Marc Gravell.

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.

  • Quick C# galley to answer :)

  • The initial idea was to "use it to decrease typing," but I know this is lazy.

  • As I said in reply, to namespaces in fact occurs the opposite, in most cases not making alias is the normal and shortest form.

5

Yes, it is possible to define alias for namespaces and classes through directive using.

using Foo = System.Globalization.CultureInfo; // Alias para a classe `CultureInfo`
using Bar = System.Globalization; // Alias para a namespace `System.Globalization`

3

In C# it is possible to define a Namespace alias, 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 on Dotnetfiddle.

And when should I use?

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

  • @Wallacemaxters Is that this is not the example I want to add, but the DotNetFiddle is giving it a go. kkk

1

Browser other questions tagged

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