user415789
user415789

Reputation:

Is it possible to make alias for a variable in C#?

Assume in a winform application we have in program.cs:

static class Program
{
    public static SomeClass someClass;

    [STAThread]
    static void Main()
    {
        //something
    }
}

so to use someClassin other classes in current Project we should access Program.someClass

the question is can we make an alias for Program.someClass like for example c1 and use c1 in code instead of Program.someClass?

Upvotes: 9

Views: 9438

Answers (3)

zerkms
zerkms

Reputation: 254916

Just pass that object to each class that need to work with it

Upvotes: 1

Christoph
Christoph

Reputation: 2004

Assuming that you want to achieve something like macros in C/C++ and also want to get rid of "Program" as naming scope, I think you cannot do that in C#.

Upvotes: 3

Paul
Paul

Reputation: 36319

If you're just looking to alias it for wrist-friendliness, do it as an inline delegate, like this:

Func<SomeClass> c1 = () => Program.someClass;

But I agree that it makes your code less clear in most cases.

Upvotes: 7

Related Questions