Length 속성은 BitArray에 포함된 요소(비트)의 개수를 가져오거나 설정하는 데 사용됩니다. 이 속성을 활용하면 배열의 크기를 손쉽게 확인할 수 있으며, 필요에 따라 크기를 동적으로 조정할 수도 있습니다.
BitArray 선언하기
먼저 다음과 같이 5개의 비트를 가지는 BitArray 객체를 생성합니다.
BitArray arr = new BitArray( 5 );
Length 속성으로 길이 확인하기
생성된 BitArray의 길이는 Length 속성으로 간단히 조회할 수 있습니다.
Console.WriteLine( "Length: {0}", arr.Length );전체 예제 코드
아래 예제를 직접 실행해 보면 BitArray 클래스의 Length 속성과 Count 속성이 어떻게 동작하는지 쉽게 이해할 수 있습니다.
using System;
using System.Collections;
public class Demo {
public static void Main() {
BitArray arr = new BitArray( 5 );
Console.WriteLine( "Count: {0}", arr.Count );
Console.WriteLine( "Length: {0}", arr.Length );
}
}실행 결과
Count: 5 Length: 5
실행 결과에서 볼 수 있듯이, 5개의 비트로 초기화된 BitArray의 Count와 Length 모두 5가 출력됩니다. 참고로 BitArray에서 Count와 Length는 항상 같은 값을 반환하지만, Length 속성은 setter를 제공하여 배열의 크기를 변경할 수 있다는 점이 다릅니다.