Я хоть убей, не могу понять, что происходит в приведенном ниже примере кода C#. Коллекция (List) Свойство тестового класса установлено только для чтения, но, тем не менее, я могу назначить его в инициализаторе объекта.
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace WF4.UnitTest
{
public class MyClass
{
private List _strCol = new List {"test1"};
public List StringCollection
{
get
{
return _strCol;
}
}
}
[TestFixture]
public class UnitTests
{
[Test]
public void MyTest()
{
MyClass c = new MyClass
{
// huh? this property is read only!
StringCollection = { "test2", "test3" }
};
// none of these things compile (as I wouldn't expect them to)
//c.StringCollection = { "test1", "test2" };
//c.StringCollection = new Collection();
// 'test1', 'test2', 'test3' is output
foreach (string s in c.StringCollection) Console.WriteLine(s);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/564 ... properties