How do I alias a class name in C#, without having to add a line of code to every file that uses the class?

Ian Boyd picture Ian Boyd · Oct 28, 2008 · Viewed 79.8k times · Source

I want to create an alias for a class name. The following syntax would be perfect:

public class LongClassNameOrOneThatContainsVersionsOrDomainSpecificName
{
   ...
}

public class MyName = LongClassNameOrOneThatContainsVersionOrDomainSpecificName;

but it won't compile.


Example

Note This example is provided for convenience only. Don't try to solve this particular problem by suggesting changing the design of the entire system. The presence, or lack, of this example doesn't change the original question.

Some existing code depends on the presence of a static class:

public static class ColorScheme
{
   ...
}

This color scheme is the Outlook 2003 color scheme. i want to introduce an Outlook 2007 color scheme, while retaining the Outlook 2003 color scheme:

public static class Outlook2003ColorScheme
{
   ...
}

public static class Outlook2007ColorScheme
{
   ...
}

But i'm still faced with the fact that the code depends on the presence of a static class called ColorScheme. My first thought was to create a ColorScheme class that I will inherit from either Outlook2003 or Outlook2007:

public static class ColorScheme : Outlook2007ColorScheme
{
}

but you cannot inherit from a static class.

My next thought was to create the static ColorScheme class, but make Outlook2003ColorScheme and Outlook2007ColorScheme classes non-static. Then a static variable in the static ColorScheme class can point to either "true" color scheme:

public static class ColorScheme
{
    private static CustomColorScheme = new Outlook2007ColorScheme();
    ...
}

private class CustomColorScheme 
{ 
   ...
}

private class Outlook2008ColorScheme : CustomColorScheme 
{
    ...
}

private class Outlook2003ColorScheme : CustomColorScheme 
{
   ...
}

but that would require me to convert a class composed entirly of readonly static Colors into overridable properties, and then my ColorScheme class would need to have the 30 different property getters thunk down into the contained object.

That's just too much typing.

So my next thought was to alias the class:

public static ColorScheme = Outlook2007ColorScheme;

But that doesn't compile.

How can I alias a static class into another name?


Update: Can someone please add the answer "You cannot do this in C#", so I can mark that as the accepted answer. Anyone else wanting the answer to the same question will find this question, the accepted answer, and a number of workarounds that might, or might not, be useful.

I just want to close this question out.

Answer

Konrad Rudolph picture Konrad Rudolph · Oct 28, 2008

You can’t. The next best thing you can do is have using declarations in the files that use the class.

For example, you could rewrite the dependent code using an import alias (as a quasi-typedef substitute):

using ColorScheme = The.Fully.Qualified.Namespace.Outlook2007ColorScheme;

Unfortunately this needs to go into every scope/file that uses the name.

I therefore don't know if this is practical in your case.