119

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.

7
  • you might as well accept Chris' answer, even if you don't want to implement it
    – devio
    Dec 2, 2008 at 14:22
  • 2
    It's not the answer, it's a workaround. The answer is that you cannot - at least until someone comes around and posts the actual syntax to do it.
    – Ian Boyd
    Dec 15, 2008 at 16:36
  • 1
    For anyone coming here, the accepted answer is incorrect as the highest rated comment works just fine in VS 2010 and VS 2017 c# projects I am working on. The fully qualified namespace must be used to specify the class when setting up the alias, but once setup the alias works within it's defined scope. Nov 21, 2017 at 18:14
  • I had to read Ian's answer and his comments in great detail before I understood what he was seeking. He wants to declare a class alias in one place, rather than having to add it to the top of every file that references the class. I'm not aware of any strongly-typed languages that support this. (If someone knows of such a language, I'd like to know about it.) I've edited the title to make this clearer. Dec 29, 2018 at 21:56
  • BTW, for anyone who is trying to do something similar: if these are classes you are defining, then the C# approach is to define an interface, which all of your classes implement. As mentioned in chills42's answer. You can then define a "service" or "factory" which returns an object which implements that interface, depending on current circumstances (e.g. platform/OS) or on a config file. Dec 29, 2018 at 22:19

12 Answers 12

165

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.

4
  • 11
    If it could go at the top of the file that contained the original class: it would be great. But the using must be added to to all broken code. And it also negates the value is having a single alias, that lets me switch all users of the existing ColorScheme class to a use a new class - without changes. In other words: i want to alias the ColorScheme class to another class.
    – Ian Boyd
    Oct 5, 2011 at 15:45
  • Is there a way to achieve the same and propagate the alias with inheritance. ie all class extending MyClass will be able to use ColorScheme instead of the.Fully.Qualified...ColorScheme by just adding the using statement in the MyClass source file? Aug 12, 2019 at 14:53
  • Well, it was very practical for my case. Finally C# will stop drawing my file paths. Jan 19, 2020 at 23:22
  • 1
    Do .net6 global usings fix the "every scope/file" limitation? Aug 26, 2022 at 2:09
25

You can make an alias for your class by adding this line of code:

using Outlook2007ColorScheme = YourNameSpace.ColorScheme;
4
  • The name 'ColorScheme' does not exist in the current context
    – Ian Boyd
    Oct 5, 2011 at 15:41
  • 7
    you need Fully.Qualified.Namespace.Of.ColorScheme
    – Jamie Pate
    Feb 2, 2013 at 21:26
  • I thought in C# could only alias namespaces (not classes) this way, where in VB.Net you are able to alias namespaces or classes using Imports. Am I wrong?
    – Nick
    May 4, 2015 at 19:47
  • You do not need fully qualified name if you put using directive inside it's namespace, e.g. when you need an alias of your own class. Feb 18, 2016 at 10:14
16

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:

enter image description here

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.

16
  • 28
    You absolutely can alias a class name in C#. "using <alias_name> = <fully_qualified_name>;"
    – clemahieu
    Jul 18, 2011 at 15:13
  • 8
    LIke @clemahieu said, you absolutely can alias a class name, you just have to use the fully qualified name. Additionally, if your aliasing a generic class, you can have to add the generic class qualifier. For example: using ShortName = MyNamespace.SubNamespace.GenericClass<MyType>; Oct 5, 2011 at 4:38
  • 13
    Downvote - you've stated "You cannot alias a class name in C#." You can. What you can't do is alias a class in the way you want to - which is a quite justified requirement, but your statement is incorrect.
    – Tom W
    Oct 26, 2011 at 13:51
  • 10
    As several posters have pointed out, having to use the qualified name in no way prohibits aliasing. You can alias the class. You just have to use the fully-qualified name to do so. You might find this inconvenient, but it doesn't make the statement 'You can't alias a class name in C#' true. Perhaps the way that aliasing works in C# is different to what you were expecting. That's fine - if that's the case, state that. But you can alias a class name in C# because the specification states that you can do so, according to the definition that it provides.
    – Tom W
    Oct 31, 2011 at 19:44
  • 7
    I like how we programmers will make our statements between the lines. What Ian is really saying is that C# is dumb and broken because it can't do a simple basic thing that anyone would want to do and should be able to do. He is correct - whether or not specific semantics make you happy.
    – IQpierce
    Dec 28, 2013 at 17:51
12

You want a (Factory|Singleton), depending on your requirements. The premise is to make it so that the client code doesn't have to know which color scheme it is getting. If the color scheme should be application wide, a singleton should be fine. If you may use a different scheme in different circumstances, a Factory pattern is probably the way to go. Either way, when the color scheme needs to change, the code only has to be changed in one place.

public interface ColorScheme {
    Color TitleBar { get; }
    Color Background{ get; }
    ...
}

public static class ColorSchemeFactory {

    private static ColorScheme scheme = new Outlook2007ColorScheme();

    public static ColorScheme GetColorScheme() { //Add applicable arguments
        return scheme;
    }
}

public class Outlook2003ColorScheme: ColorScheme {
   public Color TitleBar {
       get { return Color.LightBlue; }
   }

    public Color Background {
        get { return Color.Gray; }
    }
}

public class Outlook2007ColorScheme: ColorScheme {
   public Color TitleBar {
       get { return Color.Blue; }
   }

    public Color Background {
        get { return Color.White; }
    }
}
3
  • This looks like less of a factory and more of a weak attempt at a singleton pattern. A factory would be more likely to parameterize the creation method; you would have something more like: public static ColorScheme GetColorScheme(string descriptor);
    – OwenP
    Oct 28, 2008 at 18:33
  • True - the basic idea is to make sure than when office 2012 comes out, the code only has to change in 1 place. Oct 28, 2008 at 18:38
  • 1
    This definetly works, but it certainly is an enterprise-y solution to a simple problem.
    – Ian Boyd
    Nov 5, 2008 at 14:59
11

try this:

using ColorScheme=[fully qualified].Outlook2007ColorScheme
2
  • The name 'ColorScheme' does not exist in the current context
    – Ian Boyd
    Oct 5, 2011 at 15:41
  • 3
    you need Fully.Qualified.Namespace.Of.ColorScheme
    – Jamie Pate
    Feb 2, 2013 at 21:24
9

From C# 10 you can use global using.

global using Short = LongClassNameOrOneThatContainsVersionsOrDomainSpecificName;

And if you want to use it in MVC views, you can add the using line to _viewImports.cshtml.

The global using directive is a new feature that was recently added to C# language in C# version 10.0. This feature allows developers to declare a namespace globally in any one of the files in a project and the global declared namespace is imported and available to all files in the application. (c-sharopcorner)

2
  • 2
    This is the correct answer from 2023 for those that can use recent .NET releases. Mar 22 at 19:35
  • thanks for sharing, although this seems MS simulating VB for C#...
    – SoftWar
    Mar 27 at 10:55
6

I'm adding this comment for users finding this long after OP accepted their "answer". Aliasing in C# works by specifying the class name using it's fully qualified namespace. One defined, the alias name can be used within it's scope. Example.

using aliasClass = Fully.Qualified.Namespace.Example;
//Example being the class in the Fully.Qualified.Namespace

public class Test{

  public void Test_Function(){

    aliasClass.DoStuff();
    //aliasClass here representing the Example class thus aliasing
    //aliasClass will be in scope for all code in my Test.cs file
  }

}

Apologies for the quickly typed code but hopefully it explains how this should be implemented so that users aren't mislead into believing it cannot be done in C#.

2
  • This would be even clearer if you showed the declaration of the other class: namespace Fully.Qualified.Namespace{ public class Example { ... public void DoStuff(){ ... } ... } }. Dec 29, 2018 at 21:31
  • 2
    To be clear, this is different than what Ian is seeking. Ian has a situation where he can't (or doesn't want to) alter the source files that refer to a class. He wants a way to make a change in only one place in his application or library, resulting in what appears to be a class with the desired name, that all other code can use [without having to add that "using" statement to multiple source files - for example that source might not be available to alter]. Dec 29, 2018 at 22:14
4

Aliasing the way that you would like to do it will not work in C#. This is because aliasing is done through the using directive, which is limited to the file/namespace in question. If you have 50 files that use the old class name, that will mean 50 places to update.

That said, I think there is an easy solution to make your code change as minimal as possible. Make the ColorScheme class a facade for your calls to the actual classes with the implementation, and use the using in that file to determine which ColorScheme you use.

In other words, do this:

using CurrentColorScheme = Outlook2007ColorScheme;
public static class ColorScheme
{
   public static Color ApplyColorScheme(Color c)
   {
       return CurrentColorScheme.ApplyColorScheme(c);
   }
   public static Something DoSomethingElse(Param a, Param b)
   {
       return CurrentColorScheme.DoSomethingElse(a, b);
   }
}

Then in your code behind, change nothing:

private void button1_Click(object sender, EventArgs e)
{
   this.BackColor = ColorScheme.ApplyColorScheme(this.BackColor);
}

You can then update the values of ColorScheme by updating one line of code (using CurrentColorScheme = Outlook2008ColorScheme;).

A couple concerns here:

  • Every new method or property definition will then need to be added in two places, to the ColorScheme class and to the Outlook2007ColorScheme class. This is extra work, but if this is true legacy code, it shouldn't be a frequent occurence. As a bonus, the code in ColorScheme is so simple that any possible bug is very obvious.
  • This use of static classes doesn't seem natural to me; I probably would try to refactor the legacy code to do this differently, but I understand too that your situation may not allow that.
  • If you already have a ColorScheme class that you're replacing, this approach and any other could be a problem. I would advise that you rename that class to something like ColorSchemeOld, and then access it through using CurrentColorScheme = ColorSchemeOld;.
4

I suppose you can always inherit from the base class with nothing added

public class Child : MyReallyReallyLongNamedClass {}

UPDATE

But if you have the capability of refactoring the class itself: A class name is usually unnecessarily long due to lack of namespaces.

If you see cases as ApiLoginUser, DataBaseUser, WebPortalLoginUser, is usually indication of lack of namespace due the fear that the name User might conflict.

In this case however, you can use namespace alias ,as it has been pointed out in above posts

using LoginApi = MyCompany.Api.Login;
using AuthDB = MyCompany.DataBase.Auth;
using ViewModels = MyCompany.BananasPortal.Models;

// ...
AuthDB.User dbUser;
using ( var ctxt = new AuthDB.AuthContext() )
{
    dbUser = ctxt.Users.Find(userId);
}

var apiUser = new LoginApi.Models.User {
        Username = dbUser.EmailAddess,
        Password = "*****"
    };

LoginApi.UserSession apiUserSession = await LoginApi.Login(apiUser);
var vm = new ViewModels.User(apiUserSession.User.Details);
return View(vm);

Note how the class names are all User, but in different namespaces. Quoting PEP-20: Zen of Python:

Namespaces are one honking great idea -- let's do more of those!

Hope this helps

6
  • 2
    always, unless you cannot: e.g. sealed class
    – mikus
    Mar 18, 2016 at 11:44
  • but that won't work in many cases. e.g. public class MyList : List {} - if you later try MyList xyz = something.ToList(); you will get stuck.
    – Offler
    Jun 22, 2016 at 10:40
  • @Offler You could (and probably should) resort to either new keyword for such methods or even come up with your own ExtensionMethods, right? In any case it is my strong opinion that you should always use vanilla Collection classes (i.e. Dictionary, List, IEnumerable, IQueryable, etc.) with custom Models/ViewModels/POCOs. As Mvc states it: Convention over Configuration
    – percebus
    Jun 22, 2016 at 17:00
  • @percebus doesn't work in all cases. I try to transform vintage Code whre parts use an api no longer supported to a newer one. durign transformation code will be changed by others and should still be runable - so both should be usable just now. convering Things over 700.000 LOC (without comments) and still let it be runable would be easier, if a c++ style alias would be possible - and you only Need one place in a file to replace an alias class with the implementation from either of them .
    – Offler
    Jun 25, 2016 at 17:18
  • RE-POST for EDIT @Offler For what you are describing sound that you need somehing like a Factory with interfaces. IColorScheme oColorScheme = ColorSchemeFactory.Create(); Also, you might want to look into Dependency Injection
    – percebus
    Jun 28, 2016 at 15:13
2

Is it possible to change to using an interface?

Perhaps you could create an IColorScheme interface that all of the classes implement?

This would work well with the factory pattern as shown by Chris Marasti-Georg

1
  • It could be, but i'm not going to spend anymore time on it rather than renaming the class that is the "current" color scheme to use.
    – Ian Boyd
    Nov 5, 2008 at 14:58
0

It's a very late partial answer - but if you define the same class 'ColorScheme', in the same namespace 'Outlook', but in separate assemblies, one called Outlook2003 and the other Outlook2007, then all you need to do is reference the appropriate assembly.

0

The best way I've found to simulate alias in C# is inheritance.

Create a new class that inherits from the original class:

public class LongClassNameOrOneThatContainsVersionsOrDomainSpecificName
{
   ...
}

public class MyName 
    : LongClassNameOrOneThatContainsVersionOrDomainSpecificName
{

}

The only thing that you would need to be careful is the constructor. You need to provide a a constructor for MyName class.

public class MyName 
    : LongClassNameOrOneThatContainsVersionOrDomainSpecificName
{
    public MyName(T1 param1, T2 param2) : base(param1, param2) {}   
}

In this example I'm using T1 and T2 as generic types, since I don't know the constructor for your LongClassNameOrOneThatContainsVersionOrDomainSpecificName class.

Beware, though, that this is not alias. Doing this to you application might run into some issues or problems. You might need to create some extra code to check for types, or even overload some operators.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.