Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#의 컬렉션 클래스는 무엇입니까?

<시간/>

컬렉션 클래스는 요소에 동적으로 메모리를 할당하고 인덱스 등을 기반으로 항목 목록에 액세스하는 등 다양한 용도로 사용됩니다.

다음은 Collections −

의 클래스입니다.
Sr.No 클래스 및 설명 및 사용
1 배열 목록
개별적으로 인덱싱할 수 있는 개체의 정렬된 컬렉션을 나타냅니다.
2 해시테이블
컬렉션의 요소에 액세스하기 위해 키를 사용합니다.
3 정렬 목록
키와 인덱스를 사용하여 목록의 항목에 액세스합니다.
4 스택
후입선출 객체 컬렉션을 나타냅니다.
5 대기열
선입선출 객체 컬렉션을 나타냅니다.
6 비트배열
값 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