C# 2.0 Features - Part 2.1

Jan
23
2010
In Categories: .NET | CLR
Tags | |

You can see part 1 here

C# 2.0 came out with many features (Aliases, Anonymous Methods, Generics, Iterators, Partial Types, Property Access Modifiers, Nullable types, Conditional Operator, Null-Coalescing Operator, and Static Classes). I will talk about each feature in a separate post and how can we start using them.

In this post I will talk about Aliases. Aliases are not new keyword for those who got used to C++. It provides the same functionality as in C#. It is useful for referring to long namespace identifiers and it can be used to remove namespace collisions.

Code Sample (Please, don't try to judge the pattern nor the way I place classes. I just did so for the example):

Let's say that we have these 2 namespaces in our project

    1 namespace Business

    2 {

    3     namespace MessagesThatDidNotExpectReply

    4     {

    5         public class Mails

    6         {

    7             public static string ThankYou()

    8             {

    9                 return @"Thank you. Mail Confirmed";

   10             }

   11         }

   12     }

   13     namespace MessagesThatExpectReply

   14     {

   15         public class Mails

   16         {

   17             public static string ThankYou()

   18             {

   19                 return @"Thank you. Please, reply us to confirm your email";

   20             }

   21         }

   22     }

   23 }

 

 

When we try to access them from our class using the below code, this will cause conflict. The complier is not sure which Foo method it should use.

 

    1 using Business.MessagesThatDidNotExpectReply;

    2 using Business.MessagesThatExpectReply;

    3 namespace MainApplication

    4 {

    5     class Program

    6     {

    7         static void Main()

    8         {

    9             System.Console.WriteLine(Mails.ThankYou());

   10         }

   11     }

   12 }

or you can use alias to make your code more readable.

    1 using Automated = Business.MessagesThatExpectReply;

    2 using Manual = Business.MessagesThatDidNotExpectReply;

    3 using System;

    4 namespace MainApplication

    5 {

    6     class Program

    7     {

    8         static void Main()

    9         {

   10             Console.WriteLine(Automated.Mails.ThankYou());

   11         }

   12     }

   13 }

Here we have done three things:

1. Solved the compiler conflict
2. Used small alias instead of placing the big namespace modifier which makes the code more readable
3. Used alias name related to business which considered in my point of view a kind of documentation.

If the function is called only once we can also use alias to the class itself like this Automated = Business.MessagesThatExpectReply.Mails;

Comments

Ahmed Naguib
Ahmed Naguib Egypt
1/21/2010 2:04:58 PM Permalink

Hello Mahmoud,

This is a very nice article and very informative Smile

But please note that there is a typing mistake in item #2 "makes made" please remove one of them Laughing

Best regards,
Ahmed

Mahmoud Ghoz
Mahmoud Ghoz Egypt
1/22/2010 2:55:26 AM Permalink

Thank you ... I am happy that you like the article. I have fixed the typo, thank you again

trackback
Technical Architect blog
5/21/2010 10:21:48 PM Permalink

C# 2.0 Features - Part 2.5 Partial classes

C# 2.0 Features - Part 2.5 Partial classes

Comments are closed