什麼是C#中SortedList類的Item屬性?


排序列表是陣列和雜湊表的組合。它包含可以使用鍵或索引訪問的項列表。

獲取並設定與SortedList中特定鍵關聯的值。

你還可以使用Item屬性新增新元素。

如果鍵不存在,則可以像這樣包括在內。

myCollection["myNonexistentKey"] = myValue

如果鍵已存在,則它將用新的鍵和值覆蓋該鍵。

以下是一個示例,瞭解如何在C#中使用SorteList類的Item屬性。

示例

 實際演示

using System;
using System.Collections;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         SortedList s = new SortedList();
         s.Add("S001", "Jack");
         s.Add("S002", "Henry");
         s["S003"] = "Brad";
         // get a collection of the keys.
         ICollection key = s.Keys;
         foreach (string k in key) {
            Console.WriteLine(k + ": " + s[k]);
         }
      }
   }
}

輸出

S001: Jack
S002: Henry
S003: Brad

更新於: 23-Jun-2020

63次瀏覽

開啟您的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.