방법: foreach를 사용하여 컬렉션 클래스 액세스(C# 프로그래밍 가이드)

업데이트: 2007년 11월

다음 코드 샘플에서는 foreach와 함께 사용할 수 있는 제네릭이 아닌 컬렉션 클래스의 작성 방법을 보여 줍니다. 클래스는 C 런타임 함수 strtok_s와 유사한 문자열 토큰기입니다.

참고:

이 예제에서 보여 주는 방법은 제네릭 컬렉션 클래스를 사용할 수 없는 경우에만 사용하는 것이 좋습니다. 버전 2.0 이상의 C# 언어와 .NET Framework에서는 제네릭을 지원합니다. 이 항목의 뒷부분에서 설명하는 문제를 해결하기 위해 IEnumerable<T>을 지원하며 형식이 안전한 제네릭 컬렉션 클래스를 구현하는 방법의 예제는 방법: 제네릭 목록에 대한 반복기 블록 만들기(C# 프로그래밍 가이드)를 참조하십시오.

다음 예제의 Tokens는 ' '와 '-'를 구분 기호로 사용하여 "This is a sample sentence."라는 문장을 여러 개의 토큰으로 나눈 다음 foreach 문을 사용하여 이러한 토큰을 열거합니다.

Tokens f = new Tokens("This is a sample sentence.", new char[] {' ','-'});

foreach (string item in f)
{
    System.Console.WriteLine(item);
}

내부적으로 Tokens에서는 배열을 사용하며 이때 배열은 IEnumeratorIEnumerable 자체를 구현합니다. 코드 예제에서 배열의 열거 메서드를 자체 열거 메서드로 사용할 수도 있지만, 이는 예제의 목적에서 벗어납니다.

C#에서는 foreach와의 호환성을 위해 컬렉션 클래스가 IEnumerableIEnumerator에서 상속할 필요는 없습니다. 필수 요소인 GetEnumerator, MoveNext, ResetCurrent 멤버가 클래스에 있기만 하면 foreach를 사용할 수 있습니다. 인터페이스를 생략하면 Current의 반환 형식을 Object보다 구체적으로 정의할 수 있으므로 형식 안전성이 제공됩니다.

예를 들어, 이 항목의 앞부분에 있는 샘플 코드를 사용하여 시작하면 다음 줄이 바뀝니다.

// No longer inherits from IEnumerable:
public class Tokens  
// Doesn't return an IEnumerator:
public TokenEnumerator GetEnumerator()  
// No longer inherits from IEnumerator:
public class TokenEnumerator  
// Type-safe: returns string, not object:
public string Current  

이제 Current에서는 문자열을 반환하므로 호환되지 않는 형식이 foreach 문에 사용되면 컴파일러에서 이를 감지할 수 있습니다.

// Error: cannot convert string to int:
foreach (int item in f)  

IEnumerableIEnumerator를 생략하면 기타 공용 언어 런타임 호환 언어의 foreach 문이나 그에 상응하는 문과 컬렉션 클래스를 상호 운용할 수 없다는 단점이 있습니다.

다음 예제와 같이 IEnumerableIEnumerator에서 상속하고 명시적 인터페이스 구현을 사용하면 C# 내의 형식 안전성 및 기타 공용 언어 런타임 호환 언어와의 상호 운용성이라는 두 가지 장점을 모두 얻을 수 있습니다.

예제

using System.Collections;

// Declare the Tokens class:
public class Tokens : IEnumerable
{
    private string[] elements;

    Tokens(string source, char[] delimiters)
    {
        // Parse the string into tokens:
        elements = source.Split(delimiters);
    }

    // IEnumerable Interface Implementation:
    //   Declaration of the GetEnumerator() method 
    //   required by IEnumerable
    public IEnumerator GetEnumerator()
    {
        return new TokenEnumerator(this);
    }


    // Inner class implements IEnumerator interface:
    private class TokenEnumerator : IEnumerator
    {
        private int position = -1;
        private Tokens t;

        public TokenEnumerator(Tokens t)
        {
            this.t = t;
        }

        // Declare the MoveNext method required by IEnumerator:
        public bool MoveNext()
        {
            if (position < t.elements.Length - 1)
            {
                position++;
                return true;
            }
            else
            {
                return false;
            }
        }

        // Declare the Reset method required by IEnumerator:
        public void Reset()
        {
            position = -1;
        }

        // Declare the Current property required by IEnumerator:
        public object Current
        {
            get
            {
                return t.elements[position];
            }
        }
    }


    // Test Tokens, TokenEnumerator
    static void Main()
    {
        // Testing Tokens by breaking the string into tokens:
        Tokens f = new Tokens("This is a sample sentence.", new char[] {' ','-'});

        foreach (string item in f)
        {
            System.Console.WriteLine(item);
        }
    }
}
/* Output:
    This
    is
    a
    sample
    sentence.  
*/

참고 항목

개념

C# 프로그래밍 가이드

참조

배열(C# 프로그래밍 가이드)

컬렉션 클래스(C# 프로그래밍 가이드)

System.Collections.Generic

기타 리소스

C# 참조