How do I alias a class name in C#, without having to add a line of code to every file that uses the class?
c#, class-design
Solution
You cannot alias a class name in C#.
There are things you can do that are not aliasing a class name in C#.
But to answer the original question: you cannot alias a class name in C#.
Update: People are confused why `using` doesn't work. Example:
Form1.cs
private void button1_Click(object sender, EventArgs e)
{
this.BackColor = ColorScheme.ApplyColorScheme(this.BackColor);
}
ColorScheme.cs
class ColorScheme
{
public static Color ApplyColorScheme(Color c) { ... }
}
And everything works. Now i want to create a new class, and alias `ColorScheme` to it (so that no code needs to be modified):
ColorScheme.cs
using ColorScheme = Outlook2007ColorScheme;
class Outlook2007ColorScheme
{
public static Color ApplyColorScheme(Color c) { ... }
}
Ohh, i'm sorry. This code doesn't compile:
My question was how to alias a class in C#. It cannot be done. There are things i can do that are not aliasing a class name in C#:
- change everyone who depends on `ColorScheme` to `using` `ColorScheme` instead (code change workaround because i cannot alias)
- change everyone who depends on `ColorScheme` to use a factory pattern them a polymorphic class or interface (code change workaround because i cannot alias)
But these workarounds involve breaking existing code: not an option.
If people depend on the presence of a `ColorScheme` class, i have to actually copy/paste a `ColorScheme` class.
In other words: i cannot alias a class name in C#.
This contrasts with other object oriented languages, where i could define the alias:
ColorScheme = Outlook2007ColorScheme
and i'd be done.
Problem
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.