Search code examples
c#static-classes

Two names for a C# static class


It's extremely lazy, but I'm wondering if it's possible to have two names point to the same static class in essence.

I have a long and descriptive static class name BusinessLogicServiceUtility (as example) and it's looking rather long-winded in code.

I'd love to use BLSU as an alternative, so I tried extending the long class with my short class name:

public static class BLSU : BusinessLogicServiceUtility
{
}

Which probably looks noob but as it turns out, it doesn't work.

It looks as though I could wrap each method in the long class with a method inside the short class, but there's a lot of methods, which makes maintaining it a pain and circumvents the whole laziness goal.

Is there a way of assigning two names to one static class, or some other method simply "extending" a static class?


Solution

  • You can create an alias for a type:

    using BLSU = Your.Namespace.BusinessLogicServiceUtility;
    

    Accessing members of type:

    BLSU.SomeStaticMethod();
    

    With C# 6 you can even go further - access static members of a type without having to qualify type name at all:

    using static Your.Namespace.BusinessLogicServiceUtility;
    

    Accessing members of type:

    SomeStaticMethod();
    

    Further reading: using Directive (C# Reference)