C# 更改布尔值的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13310410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change The Default Value of Boolean
提问by eMi
I'm writing an application, where I have quite a lot Properties of Type Boolean defined:
我正在编写一个应用程序,其中定义了很多布尔类型的属性:
private bool kajmak = true;
public bool Kajmak
{
get { return kajmak ; }
set { kajmak = value; FirePropertyChanged(() => Kajmak); }
}
As you see, I set kajmakto trueat the beginning..-the reason is nonrelevant-. (You might know that the defaultvalue of a bool variable is false).
正如你看到的,我将kajmak要true在一开始..-原因是nonrelevant-。(您可能知道bool 变量的默认值是false)。
Now, is there a way, to change the default value of a boolto true? So I would write:
现在,有没有办法将 a 的默认值更改bool为true?所以我会写:
private bool kajmak; //kajmak = true
instead of
代替
private bool kajmak = true;
What could I do to achieve this?
我能做些什么来实现这一目标?
采纳答案by Davio
Because booleans are false by default, I use positive forms in my names, like IsInitialized, HasSomethingetc. which I want to be false by default until I explicitly set them.
因为默认情况下布尔值是假的,所以我在我的名字中使用正数形式,比如IsInitialized,HasSomething等等。在我明确设置它们之前,我希望默认情况下为假。
If you find you need something to be true by default, maybe you need to rename your variable so it makes more sense when the default is false.
如果你发现你需要一些东西默认为真,也许你需要重命名你的变量,这样当默认值为假时它更有意义。
回答by looper
No, there's no possibility to change the default value. If you could change the default-value, it wouldn't be the default anymore ;).
不,无法更改默认值。如果您可以更改默认值,它将不再是默认值;)。
But to set the default-value to null, you could use this:
但是要将默认值设置为 null,您可以使用以下命令:
bool? kajmak;
But that's not what you want...
但这不是你想要的......
回答by Justin Niessner
No. There's no way to change the default value assigned by .NET. Your best bet is to either assign the appropriate default in the private member:
不可以。无法更改 .NET 分配的默认值。您最好的选择是在私人成员中分配适当的默认值:
private book kajmak = false;
Or use the Constructor like you're supposed to and assign the class defaults there:
或者像您应该使用的那样使用构造函数并在那里分配类默认值:
public class SomeClass
{
public SomeClass()
{
Kajmak = false;
}
public book Kajmak { get; set; }
}
回答by Damian
You may create a class myBool that defaults to false and an implicit conversion from bool to your class.
您可以创建一个默认为 false 的类 myBool 以及从 bool 到您的类的隐式转换。
回答by KGVT
In the process of trying to do something similar, a colleague enlightened me to the bool? type. It can be true, false, or null, and does not object to being on the left side of such a comparator. This does not answer your question of how to default bool to true, but does solve your conceptual problem of wanting your variables to be definable as true by default.
在尝试做类似的事情的过程中,一位同事给我启发了bool?类型。它可以是 true、false 或 null,并且不反对位于此类比较器的左侧。这并没有回答您如何将 bool 默认为 true 的问题,但确实解决了您希望变量在默认情况下可定义为 true 的概念问题。
I only post because this was the top result when I searched, and this information was helpful to me. Hopefully it will be to others who find this page.
我发帖只是因为这是我搜索时的最佳结果,而这些信息对我很有帮助。希望其他人可以找到此页面。
回答by Adam Diament
C Sharp 6.0 has introduced a nice new way to do this:
C Sharp 6.0 引入了一种很好的新方法来做到这一点:
public bool YourBool { get; set; } = true;
This is equivalent to the old way of:
这相当于旧的方式:
private bool _yourBool = true;
public bool YourBool
{
get { return _yourBool; }
set { _yourBool = value; }
}
see this article http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx
看到这篇文章http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx
回答by Alexandre Lima
In service:
在役:
public bool Kajmak { get; set; } = true;

