When will I use an alias to use the directive?

So, I read the MSDN docs and came across them:

http://msdn.microsoft.com/en-us/library/sf0df423.aspx

What is the practical use of using an alias for directives using?

I get what I have, I just don't get WHY I could use it.

+5
source share
6 answers

This is useful if you have a class with the same name in two different namespaces. When this happens, you have two options. usingone namespace and not another (which means using the full name for another), or usingone namespace usually, and the usingother with an alias. Since the alias is shorter than the full name, it is still simpler and more convenient.

Obviously, the best option is to simply not have public classes with the same name in different namespaces, especially if there is a chance that someone wants to use both in the same class.

+9
source

For example, you can use 2 other classes in your class with the same name, but in different namespaces. For instance:

 using TextBoxForms = System.Windows.Forms.TextBox
 using TextBoxWeb = System.Web.UI.WebControls.TextBox
+5
source

, .

using NatMap = System.Collections.Generic.Dictionary<IPAddress, IPAddress[]>;
...
NatMap natMap = new NatMap();

:

void PrintNat(NatMap natMap) {
    foreach (IPAddress[] addresses in natMap) {
        foreach (IPaddress address in addresses) {
            // bla bla bla
        }
    }
}
+4

, . alias , , .

,

Foo.Bar.Foo.Bar.Foo.Bar.Foo.Bar.Foo.Bar.Foo.Bar

- ,

FooBar

, ,

+3

, , , . , , :

using ImageControl = System.Windows.Controls.Image;
using Image = System.Drawing.Image;
+3

:

  • ( ). , ( ), , .

    . . , , KeyValuePair<TKey, TValue>, System.Collections.Generic - . using MyStringIntPair = MyProject.Collections.Generic.KeyValuePair<string, int>, , MyProject.Collections.Generic.

  • interop. long. a long , , 64- Windows , int. long IntPtr, 64- Windows, .

    and. FreeType uses longvery little, so when I wrote down the bindings for it, I had to make it work with Windows 64-bit. I was looking at some kind of dynamic OS-based subclasses, but preprocessor directives and type aliases were simpler and were currently executing. An example would be the FT_BBox structure, which uses longs .

A simplified, relevant version of the FT_BBox structure is below:

using System;
using System.Runtime.InteropServices;

#if WIN64
using FT_Pos = System.Int32;
#else
using FT_Pos = System.IntPtr;
#endif

namespace SharpFont.Internal
{
    /// <summary>
    /// Internally represents a BBox.
    /// </summary>
    /// <remarks>
    /// Refer to <see cref="BBox"/> for FreeType documentation.
    /// </remarks>
    [StructLayout(LayoutKind.Sequential)]
    internal struct BBoxRec
    {
        internal FT_Pos xMin, yMin;
        internal FT_Pos xMax, yMax;
    }
}
+2
source

All Articles