pokoko222
pokoko222

Reputation: 57

C# alias for class public static member?

I have class RenderManager which has public static member TheRenderer. Instead of RenderManager.TheRenderer can I create alias for that like MyRenderer or whatever?

Thank you

Upvotes: 3

Views: 3844

Answers (4)

Steve Hobbs
Steve Hobbs

Reputation: 3326

You can create a class or method-level symbol which points to that object, but you can't create a truly global alias which points to that object, no. It would still have to be namespaced in some other object.

Locally, though (inside a function or class) you could do something like var renderer = RenderManager.TheRenderer, but you would have to do that everywhere you want to use that alias.

Upvotes: 2

Tigran
Tigran

Reputation: 62246

I don't think you can do this. The only possibility is to make an alias for Type and not for its members, something like this:

using rnd = RendererManager; 

//and in code somewhere use 

rnd.TheRenderer

Hope I right understood what you mean.

Upvotes: 1

Mo Valipour
Mo Valipour

Reputation: 13496

You can declare a variable of type Func or Action.

If TheRenderer has return value:

static Func<bool> MyRenderer = () => RenderManager.TheRenderer();

If not:

static Action MyRenderer = () => RenderManager.TheRenderer();

if TheRenderer accepts some input parameters then you should use other forms of Func<> and Action<>.

Func<OutT, In1T, In2T, ...>
Action<In1T, In2T, ...>

Upvotes: 0

George Duckett
George Duckett

Reputation: 32428

Do you just mean var MyRenderer = RenderManager.TheRenderer;? i.e. assigning the value of the static member to a variable called MyRenderer?


Not that unlike valipour's solution, if the RenderManager.TheRenderer value changes after you assign it, MyRenderer will have the old value.

Upvotes: 0

Related Questions