第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

.NET中是否有只讀通用字典?

.NET中是否有只讀通用字典?

鴻蒙傳說 2019-08-31 14:34:25
我在我的只讀屬性中返回對字典的引用。如何防止消費者更改我的數據?如果這是一個IList我可以簡單地返回它AsReadOnly。我能用字典做些什么嗎?Private _mydictionary As Dictionary(Of String, String)Public ReadOnly Property MyDictionary() As Dictionary(Of String, String)    Get        Return _mydictionary    End GetEnd Property
查看完整描述

3 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

這是一個包裝字典的簡單實現:


public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>

{

    private readonly IDictionary<TKey, TValue> _dictionary;


    public ReadOnlyDictionary()

    {

        _dictionary = new Dictionary<TKey, TValue>();

    }


    public ReadOnlyDictionary(IDictionary<TKey, TValue> dictionary)

    {

        _dictionary = dictionary;

    }


    #region IDictionary<TKey,TValue> Members


    void IDictionary<TKey, TValue>.Add(TKey key, TValue value)

    {

        throw ReadOnlyException();

    }


    public bool ContainsKey(TKey key)

    {

        return _dictionary.ContainsKey(key);

    }


    public ICollection<TKey> Keys

    {

        get { return _dictionary.Keys; }

    }


    bool IDictionary<TKey, TValue>.Remove(TKey key)

    {

        throw ReadOnlyException();

    }


    public bool TryGetValue(TKey key, out TValue value)

    {

        return _dictionary.TryGetValue(key, out value);

    }


    public ICollection<TValue> Values

    {

        get { return _dictionary.Values; }

    }


    public TValue this[TKey key]

    {

        get

        {

            return _dictionary[key];

        }

    }


    TValue IDictionary<TKey, TValue>.this[TKey key]

    {

        get

        {

            return this[key];

        }

        set

        {

            throw ReadOnlyException();

        }

    }


    #endregion


    #region ICollection<KeyValuePair<TKey,TValue>> Members


    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)

    {

        throw ReadOnlyException();

    }


    void ICollection<KeyValuePair<TKey, TValue>>.Clear()

    {

        throw ReadOnlyException();

    }


    public bool Contains(KeyValuePair<TKey, TValue> item)

    {

        return _dictionary.Contains(item);

    }


    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)

    {

        _dictionary.CopyTo(array, arrayIndex);

    }


    public int Count

    {

        get { return _dictionary.Count; }

    }


    public bool IsReadOnly

    {

        get { return true; }

    }


    bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)

    {

        throw ReadOnlyException();

    }


    #endregion


    #region IEnumerable<KeyValuePair<TKey,TValue>> Members


    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()

    {

        return _dictionary.GetEnumerator();

    }


    #endregion


    #region IEnumerable Members


    IEnumerator IEnumerable.GetEnumerator()

    {

        return GetEnumerator();

    }


    #endregion


    private static Exception ReadOnlyException()

    {

        return new NotSupportedException("This dictionary is read-only");

    }

}


查看完整回答
反對 回復 2019-08-31
?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

.NET 4.5

.NET Framework 4.5 BCL介紹ReadOnlyDictionary<TKey, TValue>(源代碼)。


由于.NET Framework 4.5 BCL不包含AsReadOnlyfor詞典,因此您需要編寫自己的(如果需要)。它將類似于以下內容,其簡單性可能突出了它為什么不是.NET 4.5的優(yōu)先級。


public static ReadOnlyDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(

    this IDictionary<TKey, TValue> dictionary)

{

    return new ReadOnlyDictionary<TKey, TValue>(dictionary);

}

.NET 4.0及更低版本

在.NET 4.5之前,沒有.NET框架類包裝Dictionary<TKey, TValue>類似于ReadOnlyCollection包裝List。但是,創(chuàng)建一個并不困難。


這是一個例子 - 如果你是谷歌的ReadOnlyDictionary,還有很多其他的。


查看完整回答
反對 回復 2019-08-31
?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

在最近的BUILD會議上宣布,自.NET 4.5以來,System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>包含了界面。證明在這里(單聲道)和這里(微軟);)


不確定是否ReadOnlyDictionary也包括在內,但至少在界面上創(chuàng)建一個暴露官方.NET通用接口的實現應該不難。



查看完整回答
反對 回復 2019-08-31
  • 3 回答
  • 0 關注
  • 534 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號