Randy Hall
Randy Hall

Reputation: 8177

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?

Upvotes: 0

Views: 1199

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

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)

Upvotes: 8

Jonesopolis
Jonesopolis

Reputation: 25370

In addition, you can use using static that will allow you to use the methods without prefixing them with the class at all:

at the top of the file :

using static BusinessLogicServiceUtility;

and say you have a method public static DoLogicThings() in your static class, you can access it directly now:

public void SomeMethod()
{
     DoLogicThings();
}

this was added in C#6.

Upvotes: 1

Related Questions