简体   繁体   English

为什么集合初始值设定项不与表达式body属性一起使用?

[英]Why collection initializer is not working together with expression body property?

I think it's best to show the code right now: 我认为现在最好显示代码:

class Foo
{
    public ICollection<int> Ints1 { get; } = new List<int>();

    public ICollection<int> Ints2 => new List<int>();
}

class Program
{
    private static void Main(string[] args)
    {
        var foo = new Foo
        {
            Ints1 = { 1, 2, 3 },
            Ints2 = { 4, 5, 6 }
        };

        foreach (var i in foo.Ints1)
            Console.WriteLine(i);

        foreach (var i in foo.Ints2)
            Console.WriteLine(i);
    }
}

Obviously the Main method should print 1, 2, 3, 4, 5, 6, but it prints 1, 2, 3 only. 显然, Main方法应该打印1,2,3,4,5,6,但它只打印1,2,3。 After initialization foo.Ints2.Count is equals to zero. 初始化后foo.Ints2.Count等于零。 Why? 为什么?

It's because of how you have defined the property Int2. 这是因为您已经定义了Int2属性。 While it is indeed a getter, it's is always returning a new list. 虽然它确实是一个吸气剂,但它总是会返回一个新列表。 Int1 is a read only auto property so it's always returning the same list. Int1是一个只读的自动属性,所以它总是返回相同的列表。 Equivalent compiler magic code removed for class Foo below: 下面为类Foo删除了等效的编译器魔术代码:

class Foo
{
    private readonly ICollection<int> ints1 = new List<int>(); 
    public ICollection<int> Ints1 { get { return this.ints1; } }

    public ICollection<int> Ints2 { get { return new List<int>(); } }
}

As you can see, all mututations to Ints2 are lost because the list is always new. 正如您所看到的,Ints2的所有变异都会丢失,因为列表总是新的。

Ints2 => new List<int>(); is short for Ints2 { get { return new List<int>(); } } Ints2 { get { return new List<int>(); } } Ints2 { get { return new List<int>(); } } . Ints2 { get { return new List<int>(); } } It returns a new empty list each time the property is read. 每次读取属性时,它都会返回一个新的空列表。 You already have the fix: your first form stores the list in a field. 您已经有了修复:您的第一个表单将列表存储在一个字段中。

每次访问Ints2属性时,它都会返回新的List<int>实例。

public ICollection<int> Ints1 { get; } = new List<int>();

This line means that the backing field returned by the property is initialized with new List<int>() . 此行表示使用new List<int>()初始化属性返回的支持字段。

What Collection initializer do is call Add method for each element, so Ints1 will have 3 elements ( 1 , 2 , 3 ). 什么集合初始化要做的就是调用Add每个元素的方法,所以Ints1将有3个元素( 123 )。


public ICollection<int> Ints2 => new List<int>();

Expression bodied means that you are defining the body of the getter , something like this: 表达身体意味着你正在定义getter的主体,如下所示:

public ICollection<int> Ints2 => new List<int>();
{
    get 
    {
        return new List<int>();
    }
}

Each time you call Ints2 an new instance is returned, thats why Count property returns 0 . 每次调用Ints2都会返回一个新实例,这就是Count属性返回0

暂无
暂无

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

相关问题 为什么集合初始化表达式需要实现IEnumerable? - Why does a collection initializer expression require IEnumerable to be implemented? 为什么集合初始化程序与getter-only属性一起使用? - Why does collection initializer work with getter-only property? 简单类型的只读自动属性:初始化程序VS表达式主体获取器 - Read-Only Auto-Property for Simple Types: Initializer VS Expression Body Getter 为什么我可以使用具有只读自动属性的匿名集合初始化程序,而我不能使用 object 初始化程序 - Why can I use an anonymous collection initializer with a read-only auto-property while I can't use an object initializer 列表属性的空集合初始值设定项导致null - Empty collection initializer for list property results in null List的集合初始化器 <myCustomClass> 不给“; 预期”编译错误。 为什么? - Collection initializer for List<myCustomClass> not working giving “; expected” compile error. Why? 为什么在对象初始值设定项中允许没有“new”的集合初始值设定项但不允许在外部? - Why is a collection initializer without `new` allowed inside an object initializer but not outside? 对象初始化,为什么要调用集合初始化 - object initialization, why collection initializer is being called 为什么显然由List &lt;&gt;的集合初始值设定项克隆对象? - Why are objects apparently cloned by collection initializer for List<>?> 为什么我们可以使用集合初始值设定项分配只读IList集合? - Why can we assign readonly IList collection with collection initializer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM