📜  C#|检查HybridDictionary是否为只读

📅  最后修改于: 2021-05-29 14:05:45             🧑  作者: Mango

HybridDictionary.IsReadOnly属性用于获取一个值,该值指示HybridDictionary是否为只读。

句法:

public bool IsReadOnly { get; }

返回值:该属性始终返回false

下面的程序说明了HybridDictionary.IsReadOnly属性的用法:

范例1:

// C# code to check whether the
// HybridDictionary is read-only.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
        myDict.Add("E", "Elephant");
        myDict.Add("F", "Fish");
  
        // To check whether the HybridDictionary
        // is read-only.
        Console.WriteLine(myDict.IsReadOnly);
    }
}

输出:

False

范例2:

// C# code to check whether the
// HybridDictionary is read-only.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("one", "1");
        myDict.Add("two", "2");
        myDict.Add("three", "3");
        myDict.Add("four", "4");
  
        // To check whether the HybridDictionary
        // is read-only.
        Console.WriteLine(myDict.IsReadOnly);
    }
}

输出:

False

笔记:

  • 创建只读集合后,不允许添加,删除或修改元素。
  • 检索此属性的值是O(1)操作。
  • 只读的集合只是一个带有包装器的集合,该包装器可防止修改该集合。因此,如果对基础集合进行了更改,则只读集合将反映这些更改。

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.hybriddictionary.isreadonly?view=netframework-4.7.2