thatOneGuy
thatOneGuy

Reputation: 10612

Re-using a class as a variable in C#

I have a reference at the top of my class like so :

using my.super.long.name.space.that.i.want.to.shorten;

Inside this namespace I want to access some classes, so for ease of use I would do the following :

using easier = my.super.long.name.space.that.i.want.to.shorten;

But then I need to access some classes within this like so :

easier.i.need.this.class.please myclass = ...

I need to use this across multiple classes so I would like to have a class of my own that contains this. Something similar to this :

public class getClasses{
   public static class1 = easier.i.need.this.class.please
   public static class2 = easier.i.need.this.class.thankyou
}

So then I could just do :

getClasses.class2 myClass2 = ....

Is this possible ?

This is heavily exaggerated to express the problem Id like to solve.

EDIT

Okay, here's a super simple explanation. I have a class located at

mynamespace.myClass

And I wish to reuse this across multiple files my using a single word variable, rather than call mynamespace.myClass. Something like this :

mynamespaceClassRef myObj = ...

Where mynamespaceClassRef = mynamespace.myClass

Upvotes: 0

Views: 136

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156978

You don't need a class to specify the shorter aliases for classes, but it seems you have a namespace complexity problem.

If you want to go with this, you could use:

using Task = System.Threading.Tasks.Task;
using StringTask = System.Threading.Tasks.Task<string>;

You can't make 'sub namespaces' inside your alias, but this should help you out most of the time.

Upvotes: 1

Related Questions