Would you call "aliasing" a feature? And is this feature only used in extremely 
complicated situations (non-standard)? YES.

This code below is from MSDN but I don't think it helps beginners learn because it is too 
transparent and/or too easy:

using System;

// Using alias directive for a class.
using AliasToMyClass = NameSpace1.MyClass;

// Using alias directive for a generic class.
using UsingAlias = NameSpace2.MyClass<int>;

namespace NameSpace1
{
    public class MyClass
    {
        public override string ToString()
        {
            return "You are in NameSpace1.MyClass.";
        }
    }

}

namespace NameSpace2
{
    class MyClass<T>
    {
        public override string ToString()
        {
            return "You are in NameSpace2.MyClass.";
        }
    }
}

namespace NameSpace3
{
    // Using directive:
    using NameSpace1;
    // Using directive:
    using NameSpace2;

    class MainClass
    {
        static void Main()
        {
            AliasToMyClass instance1 = new AliasToMyClass();
            Console.WriteLine(instance1);

            UsingAlias instance2 = new UsingAlias();
            Console.WriteLine(instance2);

        }
    }
}
// Output: 
//    You are in NameSpace1.MyClass.
//    You are in NameSpace2.MyClass.

credit to: https://msdn.microsoft.com/en-us/library/sf0df423.aspx

SO, LET'S USE THIS TYPE OF CODE FOR THE FUTURE:
using System;
using Diamond = System.Text.StringBuilder;

class Program
{
    static void Main()
    {
	Diamond diamond = new Diamond();
	diamond.Append("vals diamond");
	diamond.Append(Carats);

	Console.WriteLine(diamond);
    }
}

//Output:
//vals diamondCarats

BY, VALERIE MARTIN 2016

 

Advertisements