What is Aliases?

What is Aliases?

Aliases:-C# 2.0 introduces the concept of an "alias". This allows you to effectively name an assembly reference when you compile the code, and use that name to disambiguate between names. As well as disambiguating between identical namespace-qualified names, aliases allow you to disambiguate between names which have been declared within an already used namespace and names which belong to the "root" namespace. This is achieved with the predefined alias of global. 

Example:-
     using System;

namespace Foo.Bar
{
    public class Baz
    {
        public static void SayHiLib()
        {
            Console.WriteLine ("Hello Lib");
        }
    }
}

Baz.cs:

using System;

namespace Foo.Bar
{
    public class Baz
    {
        public static void SayHiNested()
        {
            Console.WriteLine ("Hello Nested");
        }
    }
}

class Baz
{
    public static void SayHiBaz()
    {
        Console.WriteLine ("Hello Baz");
    }
}

Test.cs:

extern alias X;

namespace Foo.Bar
{
    class Test
    {
       public static void Main()
        {
          Baz.SayHiNested();
            
          global::Baz.SayHiBaz();
            
            X::Foo.Bar.Baz.SayHiLib();
        }
    }
}    

Compile:

csc /target:library Lib.cs
csc /r:X=lib.dll Baz.cs Test.cs

By:kamal
Date:2012-10-10 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related C Links

C interview questions and answers for experienced and fresher