简体   繁体   English

静态属性的默认值

[英]default value for a static property

I like c#, but why can I do : 我喜欢c#,但为什么我可以这样做:

public static bool Initialized { private set; get; }

or this : 或这个 :

public static bool Initialized = false;

but not a mix of both in one line ? 但不是两者兼而有之?

I just need to set access level to my variable (private set), and I need it set at false on startup. 我只需要为我的变量(私有集)设置访问级别,我需要在启动时将其设置为false。 I wouldn't like to make that boring private _Initialized variable, which would be returned by the getter of the public Initialized var. 我不想制作那个无聊的私有_Initialized变量,它将由公共Initialized var的getter返回。 I like my code to be beautiful. 我喜欢我的代码很漂亮。 (NB: my variable is static, it can't be initialized in the constructor). (注意:我的变量是静态的,它不能在构造函数中初始化)。

Thanks 谢谢

You could use a static constructor 您可以使用静态构造函数

static MyClass()
{
    Initialized = false;
}

However, as has been mentioned by others the default value of a bool will be false. 但是,正如其他人所提到的,bool的默认值将为false。

从C#6开始:

public static bool Initialized { private set; get; } = false;

You can just do: 你可以这样做:

public static bool Initialized { private set; get; }

Since bool values are always false by default, there's no need to initialize it. 由于默认情况下bool值始终为false,因此无需初始化它。

If you need this to be true by default, or to have more complex logic, you need to do this in a static constructor or use a backing field. 如果默认情况下需要这个,或者要有更复杂的逻辑,则需要在静态构造函数中执行此操作或使用支持字段。

As for "I like my code to be beautiful" - personally, for non-default initialization, I think this is just as "beautiful": 至于“我喜欢我的代码是美丽的” - 就个人而言,对于非默认初始化,我认为这就像“美丽”:

private static bool initialized = true;
public static bool Initialized { get { return initialized; } }

This makes the initialization to a non default very visible, which is not a bad thing. 这使得非默认的初始化非常明显,这不是一件坏事。

The two blocks of code you have mentioned are two different things. 你提到的两个代码块是两个不同的东西。

The first block is an auto implemented property defination . 第一个块是自动实现的属性定义 This is syntactic sugar for a full property defination which looks like this: 这是完全属性定义的语法糖,如下所示:

private static bool _initialized;
public static bool Initialized
{
    private set
    {
        _initialized = value;
    }
    get
    {
        return _initialized;
    }
}

Your second block of code is a static member definition . 您的第二个代码块是静态成员定义 If you look at the expansion I have given above, you'll notice that it includes a private static member definition. 如果你看一下我上面给出的扩展,你会发现它包含一个私有的静态成员定义。 If you want to provide an initial value you can do it here: 如果您想提供初始值,可以在此处执行:

private static bool _initialized = false;
public static bool Initialized
{
    private set
    {
        _initialized = value;
    }
    get
    {
        return _initialized;
    }
}

The inline property definition you are using was designed just to make code a bit shorter in the most common case. 您正在使用的内联属性定义只是为了在最常见的情况下使代码更短。 If you want to do anything else, you can use the full form of the property code. 如果您想要做任何其他事情,可以使用完整形式的属性代码。

Alternatively, you can go down a completely different route and use a static constructor. 或者,您可以使用完全不同的路径并使用静态构造函数。 (See Corey's answer ) (见Corey的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM