简体   繁体   English

C#bool对象属性验证

[英]C# bool object property validation

I have a class Shoes with a bool property to check if shoes are secondhand or not. 我有一类具有bool属性的Shoes ,用于检查鞋子是否二手。

class Shoes
{        
    private bool secondhand;

    public bool IsSecondHand
    {
        get { return secondhand; }
        set
        {
            if (value == ) //don't know what to use here
            {
                value = false;
            }
            else
            {
               //is true
            }
        }
    }
}

My intention is to use this class (separate file) with a WPF window and using a checkbox that when checked makes my bool true, otherwise is false. 我的意图是将此类(单独的文件)与WPF窗口一起使用,并使用一个checkbox ,当选中该checkbox时,我的布尔值将为true,否则为false。 I need to preserve the format get {} set {}. 我需要保留get {} set {}的格式。 The problem is that this proprety should "work" even without the WPF part. 问题在于,即使没有WPF部分,该属性也应该“起作用”。

Just simply do: 只需做:

set
{
   secondhand = value;
}

Or, as @JFM suggests, you can simply use an auto property and you no longer need to explicitly declare the backing field: 或者,就像@JFM建议的那样,您可以简单地使用auto属性,而不再需要显式声明backing字段:

public bool IsSecondHand {get; set;}

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

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