简体   繁体   English

在类中定义属性

[英]Defining a property within a class

Many times I have seen code where they write a Class like this: 很多时候我看过代码,他们写这样的类:

public class Detail
{
    public Detail()
    {
        this.ColumnCtrl = new List<UserControl>();
    }
    public List<UserControl> ColumnCtrl { get; set; }
}

But if I only write the following – it works very fine also: 但是,如果我只写下面的内容 - 它也可以很好地工作:

public class Detail
{
    public List<UserControl> ColumnCtrl { get; set; }
}

Is there any reason to write my get-set Classes like in the first example? 有没有理由在第一个例子中编写我的get-set Classes?

Yes, because the second example will never be initialized when the class is instantiated. 是的,因为在实例化类时,永远不会初始化第二个示例。

In the first example, the following statement would work: 在第一个示例中,以下语句将起作用:

var detail = new Detail();
detail.ColumnCtrl.Add(new UserControl());

Whereas in the second it would fail with a NullReferenceException unless you initialize it yourself before using it. 在第二种情况下,它会因NullReferenceException而失败,除非您在使用它之前自己初始化它。

The second example does not create a new instance for the property, and if you access it directly after creating an instance of Detail you will throw a null reference exception. 第二个示例不为属性创建新实例,如果在创建Detail实例后直接访问它,则会抛出空引用异常。

In the first example, they are insuring this will not happen. 在第一个例子中,他们确保不会发生这种情况。

The first one always ensures that a instance of a List is returned because the constructor creates a new instance. 第一个始终确保返回List的实例,因为构造函数创建了一个新实例。 If you dont have that in the constructor it will return a null in the property. 如果你在构造函数中没有它,它将在属性中返回null。

If you then did: 如果你那么做:

Detail myInstance = new Detail();
myInstance.ColumnCtrl.Add........  //NullReferenceException is thrown here!

There is a reason to write it the way it is done in the first example. 有理由按照第一个例子中的方式编写它。 It is the initialization of the property. 它是属性的初始化。 In the second case if you type: 在第二种情况下,如果您键入:

var detail = new Detail();
var count = detail.ColumnCtrl.Count

you will get exception, because ColumnCtrl is not initialized. 你会得到异常,因为ColumnCtrl没有初始化。

You do not always need to initialize, eg there is no need to initialize when you use dependency injection or if you check for null in a ColumnCtrl getter and initialize it the first time it's requested. 您并不总是需要初始化,例如,在使用依赖项注入时无需初始化,或者在ColumnCtrl getter中检查null并在第一次请求时初始化它。

In the second situation the ColumnCtrl is not assigned a value. 在第二种情况下,ColumnCtrl未分配值。 In The first case you make sure that the ColumnCtrl is assigned an empty list by default. 在第一种情况下,您确保默认情况下为ColumnCtrl分配一个空列表。

Your first example initializes the ColumnCtrl property to a value when the Detail class is constructed, where your second one will be left null . 您的第一个示例在ColumnCtrl Detail类时将ColumnCtrl属性初始化为一个值,其中第二个示例将保留为null If it isn't initialized elsewhere you should expect NullReferenceException s. 如果它没有在其他地方初始化,你应该期待NullReferenceException

As long as you initialize the ColumnCtrl property explicitly before use when using your second example, they are exactly the same thing. 只要在使用第二个示例之前显式初始化ColumnCtrl属性,它们就完全相同。

Detail newDetail = new Detail();
newDetail.ColumnCtrl = new List<UserControl>();
newDetail.ColumnCtrl.Add(new UserControl());

An alternative (though slightly more verbose) would be to lazily instantiate the ColumnCtrl property to eliminate the overhead of instantiating it when Detail is constructed: 另一种方法(虽然稍微冗长一点)是懒惰地实例化ColumnCtrl属性,以消除构造Detail时实例化它的开销:

public class Detail
{
    public Detail()
    {
        this.ColumnCtrl = new List<UserControl>();
    }
    private List<UserControl> columnCtrl = null;
    public List<UserControl> ColumnCtrl
    {
        get 
        {
            // Missing appropriate locking mechanisms for brevity
            if (columnCtrl == null)
                columnCtrl = new List<UserControl>();

            return columnCtrl;
        }
        // The set is not absolutely necessary if you never need to set it
        // from outside of Details but if you do...
        set
        {
            columnCtrl = value;
        }
    }
}

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

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