在 C# 中檢查 SortedList 是否是隻讀的


檢查 SortedList 是否只讀的程式碼如下 −

示例

 現場演示

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add("One", "IT");
      list.Add("Two ", "Operations");
      list.Add("Three", "Marketing");
      list.Add("Four", "Purchase");
      list.Add("Five", "Sales");
      list.Add("Six", "Finance");
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("
List of values...SortedList");       IList col = list.GetValueList();       foreach(string res in col) {          Console.WriteLine(res);       }       Console.WriteLine("
SortedList is read-only? = "+list.IsReadOnly);    } }

輸出

這將產生以下輸出 −

SortedList elements...
Five Sales
Four Purchase
One IT
Six Finance
Three Marketing
Two  Operations
List of values...SortedList
Sales
Purchase
IT
Finance
Marketing
Operations
SortedList is read-only? = False

示例

讓我們看另一個示例 −

 現場演示

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add("One", "Finance");
      list.Add("Two", "Marketing");
      list.Add("Three", "Sales");
      list.Add("Four", "Purchase");
      list.Add("Five", "Operations");
      list.Add("Six", "IT");
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("
Index at key One = "+list.IndexOfKey("One"));       ICollection col = list.Keys;       Console.WriteLine("
Collection of Keys...");       foreach(string res in col)          Console.WriteLine(res);       Console.WriteLine("
SortedList is read-only? = "+list.IsReadOnly);    } }

輸出

這將產生以下輸出 −

SortedList elements...
Five Operations
Four Purchase
One Finance
Six IT
Three Sales
Two Marketing
Index at key One = 2
Collection of Keys...
Five
Four
One
Six
Three
Two
SortedList is read-only? = False

更新於: 06-12-2019

65 次瀏覽

開啟您的 職業生涯

完成課程以獲得認證

開始使用
廣告