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

后端 未结 7 1361
栀梦
栀梦 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<T> AsReallyReadOnly<T>(this IEnumerable<T> source)
    {
        foreach (T t in source) yield return t;
    }
    

    if I add to Earwicker's example

    ...
    IEnumerable<int> f = a.AsReallyReadOnly();
    IList<int> 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.

    0 讨论(0)
提交回复
热议问题