Я хочу предоставить коллекцию, которую можно редактировать в классе владельца< /code>, но доступен для чтения только для других общедоступных областей.
Пробная версия 1:
Код: Выделить всё
public class MyClass
{
private List _myList = new List();
public IEnumerable MyList { get { return _myList; } }
}
Код: Выделить всё
var x = ((List)MyList);
Код: Выделить всё
public class MyClass
{
private List _myList = new List();
public IEnumerable MyList { get { return _myList.ToList(); } }
}
Испытание 3:
Код: Выделить всё
public class MyClass
{
private List _myList = new List();
private ReadOnlyCollection _roList =
new ReadOnlyCollection(_myList)
public IEnumerable MyList { get { return _roList; } }
}
Код: Выделить всё
Debug Trace:
Use normal foreach on the ReadOnlyCollection
Speed(ms): 2520,3203125
Result: 4999999950000000
use.ForEach
Speed(ms): 1446,1796875
Result: 4999999950000000
Use normal foreach on the List
Speed(ms): 1891,2421875
Result: 4999999950000000
Подробнее здесь: https://stackoverflow.com/questions/132 ... s-readonly