【问题标题】:C# use static class properties as if they were namespaced?C# 使用静态类属性,就好像它们被命名空间一样?
【发布时间】:2019-10-22 22:10:34
【问题描述】:

我正在尝试创建一个类来保存我所有的全局常量,例如:

namespace MyProj
{
    public static class Constants
    {
        public const string MY_STRING = "this needs to be used ad nauseum";
    }
}

这很好用。但是,结果是在代码中我必须始终键入:

doSomethingWith(Constants.MY_STRING);

当我真正想做的是去(类似):

using MyProj.Constants;
doSomethingWith(MY_STRING);

我怎样才能做到这一点?

【问题讨论】:

    标签: c# namespaces using


    【解决方案1】:

    static 添加到您的using

    using static MyProj.Constants;
    

    详情here

    【讨论】:

    • 每天学习新东西!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 2011-03-23
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多