yield használata

Filed Under (Uncategorized) by nameless on 07-08-2009

yield használata:

using System;
using System.Collections;
using System.Collections.Generic;
 
namespace Program
{
    class ArrayClass : IEnumerable
    {
        int[] array = { 23, 34, 5643, 56767 };
 
        public IEnumerator GetEnumerator()
        {
            for (int i = 0; i < array.Length; i++) //végigpörgetük a tömb elemeit
                yield return array[i]; //visszaadjuk a tömb adott elemét a yield return segítségével
        }
    }
    class MainClass
    {
        public static void Main()
        {
            var myA = new ArrayClass();
 
            foreach (var i in myA)
            {
                Console.WriteLine(i);
            }
 
            Console.ReadKey();
        }
    }
}

Post a comment