开发者

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

开发者 https://www.devze.com 2023-02-03 01:58 出处:网络
Assume in a winform application we have in program.cs: static class Program { public static SomeClass someClass;

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 someClass开发者_高级运维in 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?


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.


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#.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号