C# · CodeProject · Dotnet

Aliasing some thing via Using in Csharp Internals TIP (Beginner)

Dear Reader,

As again with my interviews questions i have been asking to few people here, where in they would not answer the tricky questions like this posts title. Again do not get surprised by the title words i.e some thing.

You can not alias every thing in C#, only types such as Class, Struct, Enums, etc. can be done. Due to my craziness/curiousness (as you may refer too) i tried various combinations of doing aliasing. So on that, you can not alias methods nor you can not alias and alias (yep it does not make sense) πŸ™‚

So in this post i just started digging inside and saw whats actually the compiler is doing in handling an alias name, at first (in my fresher days) i thought CLR may be at run time deciphering the alias name that means the compiler is just carry forwarding the alias directly into IL.

So as per the below code:

using SomeClass = ConsoleApp.Apps.Samples.Others.SomeThings.MyClass;
namespace ConsoleApp.Apps.Samples.Others.SomeThings
{
    class MyClass
    {
        public struct MyStrcut
        {

        }
    }
}

Where the compiler just transforms SomeClass alias name to actual Namespace/class or in other words the compiler transforms LHR value with the RHS value inside IL as shown below image:

So that means, the alias name will not be carry forwarded into the IL or in other words CLR will not have any idea about it. Its all compiler game going on.

Happy Coding πŸ™‚

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s