How should I use properties when dealing with read-only List members

后端 未结 7 1463
栀梦
栀梦 2020-12-01 08:26

When I want to make a value type read-only outside of my class I do this:

public class myClassInt
{
    private int m_i;
    public int i {
        get { ret         


        
7条回答
  •  渐次进展
    2020-12-01 09:23

    public static IEnumerable AsReallyReadOnly(this IEnumerable source)
    {
        foreach (T t in source) yield return t;
    }
    

    if I add to Earwicker's example

    ...
    IEnumerable f = a.AsReallyReadOnly();
    IList g = f.AsWritable(); // finally can't get around it
    
    g.Add(8);
    Debug.Assert(a.Count == 78);
    

    I get InvalidOperationException: Sequence contains no matching element.

提交回复
热议问题