استخدام تكرارات (البرمجة C# إرشادات)

The most عام way إلى إنشاء an المكرر هو إلى implement the GetEnumerator أسلوب on the IEnumerable واجهة, for مثال:

public System.Collections.IEnumerator GetEnumerator()
{
    for (int i = 0; i < 10; i++)
    {
        yield return i;
    }
}

وجود GetEnumeratorالنوع يجعل الأسلوب على نوع قابل للتعداد و يسمح باستخدام كشف foreach . إذا كان الأسلوب أعلاه جزءا من تعريف فئة ل ListClass، قد يكون من الممكن إلى استخدام foreachتشغيل الفئة كما يلي:

static void Main()
{
    ListClass listClass1 = new ListClass();

    foreach (int i in listClass1)
    {
        System.Console.Write(i + " ");
    }
    // Output: 0 1 2 3 4 5 6 7 8 9
}

foreachبيان باستدعاء ListClass.GetEnumerator()و enumeraإلىr التي تم إرجاعها باستخدام إلى تكرار من خلال قيم. للحصول على مثال عن كيفية إنشاء عام المكرر الذي يرجع IEnumerator<T>الواجهة، راجع كيفية القيام بما يلي: إنشاء كتلة مكرر للقائمة العامة (دليل البرمجة لـ #C).

هو أيضا إمكانية استخدام تكرارات المسماة لاعتماد طرقاً مختلفة للتكرار عبر نفس مجموعة بيانات. على سبيل المثال، قد يوفر أحد المكرر التي تقوم بإرجاع العناصر في ترتيب تصاعدي ترتيب، والتي تقوم بإرجاع العناصر بترتيب تنازلي. يمكن أن يكون iteraإلىr المعلمات إلى تمكين عملاء إلى التحكم بالكامل أو جزء من سلوك المكرر. المكرر التالي بتنفيذ IEnumerableواجهة استخدام المكرر المسماة SampleIterator:

// Implementing the enumerable pattern
public System.Collections.IEnumerable SampleIterator(int start, int end)
{
    for (int i = start; i <= end; i++)
    {
        yield return i;
    }
}

تكرار المسماة هو استدعاء مثل th هو:

ListClass test = new ListClass();

foreach (int n in test.SampleIterator(1, 10))
{
    System.Console.Write(n + " ");
}
// Output: 1 2 3 4 5 6 7 8 9 10

يمكنك استخدام المزيد العائد عبارة في نفس المكرر كما في المثال التالي:

public System.Collections.IEnumerator GetEnumerator()
{
    yield return "With an iterator, ";
    yield return "more than one ";
    yield return "value can be returned";
    yield return ".";
}

ثم يمكنك طباعة نتائج استخدام التالية foreachكشف:

foreach (string element in new TestClass())
{
    System.Console.Write(element);
}
// Output: With an iterator, more than one value can be returned.

وهذا مثال يعرض نص التالي:

With an iterator, more than one value can be returned.

On each successive iteration of the foreach loop (or the direct call to IEnumerator.MoveNext), the next iterator code body resumes after the previous yield statement and continues to the next until the end of the iterator body is reached, or a yield break statement is encountered.

لا تدعم تكرارات IEnumerator.Resetأسلوب. إلى re-يكرر من البداية، يجب أن تحصل iteraإلىr جديدة.

مثال

تحتوي التعليمة البرمجية التالية على الجميع أمثلة من هذا الموضوع.

namespace UsingIterators
{
    class Program
    {
        static void Main()
        {
            // Using a simple iterator.
            ListClass listClass1 = new ListClass();

            foreach (int i in listClass1)
            {
                System.Console.Write(i + " ");
            }
            // Output: 0 1 2 3 4 5 6 7 8 9
            System.Console.WriteLine();


            // Using a named iterator.
            ListClass test = new ListClass();

            foreach (int n in test.SampleIterator(1, 10))
            {
                System.Console.Write(n + " ");
            }
            // Output: 1 2 3 4 5 6 7 8 9 10
            System.Console.WriteLine();


            // Using multiple yield statements.
            foreach (string element in new TestClass())
            {
                System.Console.Write(element);
            }
            // Output: With an iterator, more than one value can be returned.
            System.Console.WriteLine();

        }
    }

    class ListClass : System.Collections.IEnumerable
    {

        public System.Collections.IEnumerator GetEnumerator()
        {
            for (int i = 0; i < 10; i++)
            {
                yield return i;
            }
        }

        // Implementing the enumerable pattern
        public System.Collections.IEnumerable SampleIterator(int start, int end)
        {
            for (int i = start; i <= end; i++)
            {
                yield return i;
            }
        }
    }

    class TestClass : System.Collections.IEnumerable
    {
        public System.Collections.IEnumerator GetEnumerator()
        {
            yield return "With an iterator, ";
            yield return "more than one ";
            yield return "value can be returned";
            yield return ".";
        }
    }
}

راجع أيضًا:

المهام

كيفية القيام بما يلي: قم بإنشاء المكرر حظر لقائمة إعداد صحيحة (دليل البرمجة C#)

كيفية القيام بما يلي: إنشاء كتلة مكرر للقائمة العامة (دليل البرمجة لـ #C)

المرجع

العائد (C# مرجع)

استخدام foreach مع صفائف ( ارشادات برمجة C# )

foreach في ( مرجع C# )

المبادئ

دليل البرمجة لـ #C