Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C#에서 배열의 크기가 고정되어 있는지 확인하는 방법

C#에서 배열이 고정 크기(fixed size)를 갖는지 확인하려면 IsFixedSize 속성을 사용하면 됩니다. 이 속성은 해당 배열의 크기가 고정되어 있으면 true, 그렇지 않으면 false를 반환합니다.

IList.IsFixedSize 인터페이스를 통해 제공되는 이 속성은 일반적인 배열(new 키워드로 생성된 배열)에서는 항상 true를 반환한다는 점을 기억해 두면 좋습니다. 아래 예제 코드를 통해 직접 확인해 보겠습니다.

예제 1: 요소가 있는 배열 확인하기

using System;
public class Demo {
   public static void Main(){
      string[] products = new string[] { "Electronics", "Accessories", "Clothing", "Toys", "Clothing", "Furniture" };
      Console.WriteLine("Products list...");
      foreach(string s in products){
         Console.WriteLine(s);
      }
      Console.WriteLine("Is the products Accessories in the array? = {0}",
         Array.Exists(products, ele => ele == "Accessories"));
      Console.WriteLine("Is the products Stationery in the array? = {0}",
         Array.Exists(products, ele => ele == "Stationery"));
      Console.WriteLine("\nOne or more products begin with the letter 'C'? = {0}",
         Array.Exists(products, ele => ele.StartsWith("C")));
      Console.WriteLine("One or more planets begin with 'D'? = {0}",
         Array.Exists(products, ele => ele.StartsWith("D")));
      Console.WriteLine("One or more products begin with the letter 'T'? = {0}",
         Array.Exists(products, ele => ele.StartsWith("T")));
      Console.WriteLine("One or more planets begin with 'E'? = {0}",
         Array.Exists(products, ele => ele.StartsWith("E")));
      Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);
   }
}

위 코드에서는 문자열 배열 products를 생성한 뒤, Array.Exists() 메서드로 특정 요소의 존재 여부와 특정 문자로 시작하는 요소가 있는지를 확인하고, 마지막에 IsFixedSize 속성으로 배열의 크기가 고정되어 있는지 출력합니다.

출력 결과

Products list...
Electronics
Accessories
Clothing
Toys
Clothing
Furniture
Is the products Accessories in the array? = True
Is the products Stationery in the array? = False
One or more products begin with the letter 'C'? = True
One or more planets begin with 'D'? = False
One or more products begin with the letter 'T'? = True
One or more planets begin with 'E'? = True
Is the array having fixed size? = True

실행 결과를 보면 배열에 "Accessories"는 존재하지만 "Stationery"는 존재하지 않으며, 'C', 'T', 'E'로 시작하는 요소는 있지만 'D'로 시작하는 요소는 없음을 알 수 있습니다. 그리고 마지막 줄에서 IsFixedSize 속성이 True를 반환한 것을 통해 이 배열의 크기가 고정되어 있음을 확인할 수 있습니다.

예제 2: 빈 배열인 경우

이번에는 요소가 하나도 없는 빈 배열로 같은 속성을 확인해 보겠습니다.

using System;
public class Demo {
   public static void Main(){
      string[] products = new string[] { };
      Console.WriteLine("One or more planets begin with 'E'? = {0}",
      Array.Exists(products, ele => ele.StartsWith("E")));
      Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);
   }
}

출력 결과

One or more planets begin with 'E'? = False
Is the array having fixed size? = True

배열이 비어 있어 조건에 맞는 요소는 없으므로 Array.Exists()는 false를 반환했지만, 빈 배열 역시 생성 시점에 크기가 0으로 고정되기 때문에 IsFixedSize는 여전히 True를 반환합니다.

정리

C#에서 일반적인 배열은 한 번 생성되면 크기를 변경할 수 없습니다. 따라서 IsFixedSize 속성은 대부분의 경우 true를 반환하며, 동적으로 크기를 조절해야 한다면 List<T>와 같은 컬렉션을 사용하는 것이 좋습니다.