If I have a class that contains, for example, a List
You won't be able to use an autoproperty.
public class SomeClass()
{
private List<string> someList;
public IList<string> SomeList {
get { return someList.AsReadOnly(); }
}
}
public class SomeClass()
{
private List<string> _someList = new List<string>();
public IList<string> SomeList
{
get { return _someList.AsReadOnly(); }
}
}
Return IEnumerable<string>
, which is immutable. The getter should look like this:
public IEnumerable<string> SomeList
{
get
{
foreach(string s in someList) yield return s; // excuse my inline style here, zealots
yield break;
}
}
You'll want to return the list as a ReadOnly list. You can do this with the following code:
using System.Collections.ObjectModel;
public ReadOnlyCollection<string> GetList() {
return SomeList.AsReadOnly();
}