对于静态属性默认值 [英] default value for a static property

查看:178
本文介绍了对于静态属性默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢C#的,但我为什么可以这样做:

I like c#, but why can I do :

public static bool Initialized { private set; get; }

或这样的:

public static bool Initialized = false;



但不能同时在一个线组合?

but not a mix of both in one line ?

我只需要访问级别设置为我的变量(私人设置),我需要将其设置为FALSE在启动时。我不喜欢让枯燥的私人_Initialized变量,它会被公众初始化VAR的getter返回。我喜欢我的代码是美丽的。 (注:我的变量是静态的,它不能在构造进行初始化)

I just need to set access level to my variable (private set), and I need it set at false on startup. I wouldn't like to make that boring private _Initialized variable, which would be returned by the getter of the public Initialized var. I like my code to be beautiful. (NB: my variable is static, it can't be initialized in the constructor).

由于

推荐答案

您可以使用静态构造函数

static MyClass()
{
    Initialized = false;
}



然而,正如已被他人提一个布尔值的默认值将是假的。

However, as has been mentioned by others the default value of a bool will be false.

这篇关于对于静态属性默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆