简体   繁体   English

更改布尔值的默认值

[英]Change The Default Value of Boolean

I'm writing an application, where I have quite a lot Properties of Type Boolean defined: 我正在编写一个应用程序,我在其中定义了很多类型为Boolean的属性:

    private bool kajmak = true;
    public bool Kajmak
    {
        get { return kajmak ; }
        set { kajmak = value; FirePropertyChanged(() => Kajmak); }
    }

As you see, I set kajmak to true at the beginning..-the reason is nonrelevant-. 如你所见,我在开始时将kajmak设置为true ..-原因是无关紧要的 - 。 (You might know that the default value of a bool variable is false ). (您可能知道bool变量的默认值为false )。

Now, is there a way, to change the default value of a bool to true ? 现在,有没有办法将bool的默认值更改为true So I would write: 所以我会写:

private bool kajmak; //kajmak = true

instead of 代替

private bool kajmak = true;

What could I do to achieve this? 我能做些什么来实现这个目标?

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

Because booleans are false by default, I use positive forms in my names, like IsInitialized , HasSomething etc. which I want to be false by default until I explicitly set them. 因为默认情况下布尔值是假的,所以我在我的名字中使用正面形式,比如IsInitializedHasSomething等我默认是假的,直到我明确设置它们为止。

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. 如果您发现默认情况下需要某些内容,则可能需要重命名变量,以便在默认值为false时更有意义。

No. There's no way to change the default value assigned by .NET. 不可以。无法更改.NET指定的默认值。 Your best bet is to either assign the appropriate default in the private member: 您最好的选择是在私有成员中分配适当的默认值:

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; }
}

在服务中:

public bool Kajmak { get; set; } = true;

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: 但是要将default-value设置为null,您可以使用:

bool? kajmak;

But that's not what you want... 但那不是你想要的......

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. 它可以是true,false或null,并且不反对位于这样的比较器的左侧。 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的问题,但确实解决了你希望你的变量在默认情况下可以定义为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. 希望有人会找到这个页面。

您可以创建一个默认为false的myBool类,以及从bool到您的类的隐式转换。

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

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