首頁  >  文章  >  後端開發  >  C# 中的集合類別是什麼?

C# 中的集合類別是什麼?

WBOY
WBOY轉載
2023-09-08 17:25:021068瀏覽

C# 中的集合类是什么?

集合類別具有各種用途,例如動態分配記憶體給元素,根據索引存取項目清單等。

以下是Collections中的類別:

序號 類別和描述和用法
1 ArrayList

它表示一個可以單獨索引的物件有序的集合。

2 Hashtable

它使用鍵來存取集合中的元素。

3 SortedList

它使用鍵和索引來存取清單中的項目。

4 Stack

#它表示一個後進先出的物件集合。

5 Queue

它表示一個先進先出的物件集合。

6 BitArray

它表示使用值1和0的二進位表示的陣列。

讓我們來看一個C#中BitArray類別的範例:

範例

 線上示範

using System;
using System.Collections;

namespace CollectionsApplication {
   class Program {
      static void Main(string[] args) {
         //creating two bit arrays of size 8
         BitArray ba1 = new BitArray(8);
         BitArray ba2 = new BitArray(8);

         byte[] a = { 60 };
         byte[] b = { 13 };

         //storing the values 60, and 13 into the bit arrays
         ba1 = new BitArray(a);
         ba2 = new BitArray(b);

         //content of ba1
         Console.WriteLine("Bit array ba1: 60");

         for (int i = 0; i < ba1.Count; i++) {
            Console.Write("{0, -6} ", ba1[i]);
         }
   
         Console.WriteLine();

         //content of ba2
         Console.WriteLine("Bit array ba2: 13");

         for (int i = 0; i < ba2.Count; i++) {
            Console.Write("{0, -6} ", ba2[i]);
         }

         Console.WriteLine();
         BitArray ba3 = new BitArray(8);
         ba3 = ba1.And(ba2);

         //content of ba3
         Console.WriteLine("Bit array ba3 after AND operation: 12");

         for (int i = 0; i < ba3.Count; i++) {
            Console.Write("{0, -6} ", ba3[i]);
         }

         Console.WriteLine();
         ba3 = ba1.Or(ba2);

         //content of ba3
         Console.WriteLine("Bit array ba3 after OR operation: 61");

         for (int i = 0; i < ba3.Count; i++) {
            Console.Write("{0, -6} ", ba3[i]);
         }

         Console.WriteLine();
   
         Console.ReadKey();
      }
   }
}

輸出

Bit array ba1: 60
False False True True True True False False
Bit array ba2: 13
True False True True False False False False
Bit array ba3 after AND operation: 12
False False True True False False False False
Bit array ba3 after OR operation: 61
True False True True False False False False
#

以上是C# 中的集合類別是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除