我想使用这个类中的const变量:
class foo
{
public const bool x = true;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在不做foo.x的情况下使用x?
我想像这样使用它:
if( x )
Run Code Online (Sandbox Code Playgroud)
而不是这样的:
if( foo.x )
Run Code Online (Sandbox Code Playgroud)
Mar*_*zek 10
要实现这一目标的唯一方法是,以纪念foo静态和使用using static要具有访问foo.x的x.
namespace Foo
{
static class Bar
{
public const bool x = true;
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
using static Foo.Bar;
Console.WriteLine(x);
Run Code Online (Sandbox Code Playgroud)
using static 是一个C#功能,所以请确保在使用之前使用C#6.
| 归档时间: |
|
| 查看次数: |
77 次 |
| 最近记录: |