IEnumerator Interface | IEnumerable Interface

IEnumerator Interface

जिस Class या Structure में IEnumerator Interface को Implement किया जाता है, उसे Enumerator कहा जाता है और Enumerator में सामान्‍यत: MoveNext() Reset() Method को Implement किया जाता है, जबकि Current नाम की एक Property होती है, जो कि Enumerator के Current Item की Position को Refer करता है।

Current Property एक एक Read-Only Property होता है और ये Property object Type का एक Reference Return करता है, इसलिए इसके माध्‍यम से Enumerator से किसी भी प्रकार के Object को Return किया जा सकता है।

MoveNext() Method, Current Collection में Enumerator की Position को आगे बढाता है, साथ ही ये एक Boolean Value भी Return करता है, जो इस बात को Indicate करता है कि Collection के विभिन्न Items पर One by One Navigate करने पर Program Control जिस Position पर पहुंचता है, वह Collection के एक Valid Item की Position है या नहीं।

यदि Program Control Collection के एक Valid Item पर पहुंचता है, तो ये Method true Return करता है। जबकि यदि Program Control Collection के एक Valid Item पर नहीं पहुंचता यानी यदि Collection के अन्तिम Item से आगे की स्थिति में पहुंच जाता है, तो ये Method false Return करता है।

जबकि Reset() नाम का तीसरा Method Collection की Initial State को फिर से Reset कर देता है, ताकि Program Control फिर से Collection के First Item को Point करने लगे। Enumerator के Concept को हम निम्न चित्र द्वारा बेहतर तरीके से समझ सकते हैं:

IEnumerable Interface - Hindi

इस चित्र के Left Part में तीन Items व उसके Enumerator को Specify किया गया है, जो कि Right Side में Show हो रहा है। इस चित्र के अनुसार हम ये मान रहे हैं कि Enumerator, ArrEnumerator नाम की Class का एक Instance है।

कोई Enumerator किस तरह से अपने Collection के Current Item का Track रखता है, ये बात पूरी तरह से Enumerator के Implementation पर Depend होती है। हम इसे किसी Object के Reference के रूप में Implement कर सकते हैं, Index Value के रूप में Implement कर सकते हैं अथवा किसी और तरीके से भी Implement कर सकते हैं।

अगले चित्र में किसी 3 Items के Collection के लिए Specify किए गए एक Enumerator की विभिन्न States को दर्शाया गया है और सभी स्थितियों को एक Number द्वारा Represent किया गया है:

IEnumerable Interface - Hindi

जैसाकि हम Enumerator की पहली स्थिति में देख सकते हैं कि Enumerator की Initial Position -1 है। यानी Enumerator अपने Collection के First Item से भी पहले Placed है। फिर जैसे-जैसे MoveNext() Method Call होता है, क्रम से Collection का अगला Item Point होता रहता है, यानी Position Change होती रहती है।

लेकिन जब Collection की Position चित्र संख्‍या 4 से 5 की तरफ Move होती है, तो MoveNext() Method Collection के अन्तिम Item से भी आगे बढ जाता है। परिणामस्वरूप इस पांचवे चित्र के अनुसार MoveNext() Method false Return करता है।

Enumerator हमें हमारे द्वारा बना, गए किसी Custom Collection के विभिन्न Items पर MoveNext()Current Members द्वारा foreach Loop के माध्‍यम से Navigate करने की सुविधा Provide करता है।

उदाहरण के लिए हम जानते हैं कि System.Array यानी Array Enumerable होते हैं, क्योंकि इनमें भी Internally IEnumerable Interface को Implement किया गया होता है। इसलिए निम्न Code Array के Elements को Display करने के लिए जो काम Manually करता है, वही काम foreach Statement Internally Automatically करता है। बल्कि C# Compiler foreach Statement को Use करने पर ऐसे ही Code Internally स्वयं Generate करता है।

File Name: EnumeratorBasics.cs
using System;
using System.Collections;

namespace CSharpInterface
{
    class EnumeratorBasics
    {
        static void Main()
        {
            int[] MyArray = { 10, 11, 12, 13 }; 	// Create an array.
            IEnumerator ie = MyArray.GetEnumerator();

            while (ie.MoveNext())
            {
                int i = (int)ie.Current;
                Console.WriteLine("{0}", i); 	// Write it out.
            }
        }
    }
}

// Output:
   10
   11
   12
   13

हम समझ सकते हैं कि ये Code Exactly वही Output Generate कर रहा है, जो Array के विभिन्न Elements को Access करने के लिए foreach Loop Internally करता है। यानी हम चाहें, तो इसी Program को हम निम्नानुसार तरीके से भी लिख सकते हैं:

File Name: foreachEnumeratorBasics.cs
using System;
using System.Collections;

namespace CSharpInterface
{
    class EnumeratorBasics
    {
        static void Main()
        {
            int[] MyArray = { 10, 11, 12, 13 }; 		// Create an array.

            foreach (int i in MyArray)
                Console.WriteLine("{0}", i); 		// Write it out.
          }
    }
}

// Output:
   10
   11 
   12
   13

दोनों Programs व उनके Output द्वारा हम समझ सकते हैं कि जो काम पिछले Program में निम्न Code कर रहा है:

            IEnumerator ie = MyArray.GetEnumerator();

            while (ie.MoveNext())
            {
                int i = (int)ie.Current;
                Console.WriteLine("{0}", i); 	// Write it out.
            }

वही काम इस Program में निम्नानुसार foreach Loop कर रहा है:

            foreach (int i in MyArray)
                Console.WriteLine("{0}", i); 		// Write it out.

यदि हम सरल शब्दों में कहें, तो जब हम किसी Class में IEnumerable व IEnumerator Interfaces को Implement करते हैं for फिर उस Collection के लिए foreach Loop को Use करते हैं, तो वास्तव में foreach Statement के लिए Internally पहले Program में Use किया गया उपरोक्त Code ही Run होता है।

IEnumerable Interface

Enumerable Class वह Class होती है, जिसमें IEnumerable Interface को Implement किया गया होता है। IEnumerable Interface में GetEnumerator() नाम का केवल एक Method Specify किया गया है, जो कि किसी Object के लिए एक Enumerator Return करता है।

उदाहरण के लिए मानलो कि MyClass नाम की एक Class में तीन Items हैं, जिन्हें Enumerate करने के लिए IEnumerable Interface को Implement करते हुए GetEnumerator() Method को Define किया गया है। ताकि हम इस Class में Specified इन तीनों Data Items को foreach Loop द्वारा Access कर सकें। इस Enumerator को हम निम्न चित्रानुसार आसानी से समझ सकते हैं:

IEnumerator Interface - Hindi

MyClass नाम की हमारी उपरोक्त चित्रानुसार Specified Class को Enumerable Class के रूप में Define करने के लिए हमें इसमें IEnumerable Interface को Implement करना होता है। ये काम हम कुछ निम्नानुसार कर सकते हैं:

	class MyClass : IEnumerable
	{
		public IEnumerator GetEnumerator 
		{
			. . . 
		}
	}

ऐसा जरूरी नहीं है कि हम हमारी Collection Class में ही IEnumerator IEnumerable दोनों Interfaces को Implement करें। बल्कि हम दो अलग Classes में इन्हें Implement कर सकते हैं।

यानी हमारी जो मुख्‍य Collection Class होती है, जिसके Items को हमें Return करना होता है, उस Class में हमें IEnumerable Interface को Implement करना होता है। क्योंकि GetEnumerator() इसी Interface का Method है और ये Method ही Current Collection के Items को One by One Enumerator के रूप में Return करता है। जबकि जिस Class में हम IEnumerator Interface के MoveNext() तथा Reset() Methods व Current Property को Implement करते हैं, उस Class के Object के रूप में हमें Collection Object को Return करना होता है।

यदि इस Concept के आधार पर हम चाहें, तो हम निम्नानुसार तरीके से MyColors नाम का एक Enumerator यानी Custom Collection बना सकते हैं, जिसमें IEnumerable Interface को Implement किया गया है, जो कि ColorEnumerator Class के एक Object के रूप में इस Spectrum नाम के Collection Object को Return करता है। चूंकि इसी ColorEnumerator नाम की Class में MoveNext() Reset() Methods तथा Current Property को Define किया गया है।

File Name: IEnumerableAndIEnumeratorInterfaceImplementationInDifferentClass.cs
using System;
using System.Collections;

namespace CSharpInterface
{
    class ColorEnumerator : IEnumerator
    {
        string[] colors;
        int position = -1;
        public ColorEnumerator(string[] theColors) 	// Constructor
        {
            colors = new string[theColors.Length];
            for (int i = 0; i < theColors.Length; i++)
                colors[i] = theColors[i];
        }

        public object Current			// Implement Current.
        {
            get
            {
                if (position == -1)
                    throw new InvalidOperationException();
                if (position >= colors.Length)
                    throw new InvalidOperationException();
                return colors[position];
            }
        }

        public bool MoveNext() 			// Implement MoveNext.
        {
            if (position < colors.Length - 1)
            {
                position++;
                return true;
            }
            else
                return false;
        }

        public void Reset() 			// Implement Reset.
        {
            position = -1;
        }
    }

    class Spectrum : IEnumerable
    {
        string[] Colors = { "violet", "blue", "cyan", "green", "yellow", "orange", "red" };
        public IEnumerator GetEnumerator()
        {
            return new ColorEnumerator(Colors);
        }
    }

    class Program
    {
        static void Main()
        {
            Spectrum spectrum = new Spectrum();
            foreach (string color in spectrum)
                Console.WriteLine(color);
        }
    }
}

// Output:
   violet
   blue
   cyan
   green
   yellow
   orange
   red

इस Program में जब Main() Method Execute होता है, तो सबसे पहले Spectrum Type का एक Object Create करता है।

चूंकि Spectrum Class में हमने IEnumerable Interface के GetEnumerator() Method को Implement किया है। इसलिए Spectrum Class एक प्रकार का User Defined Custom Collection है, जो निम्न Statement द्वारा foreach Loop के हर Iteration में एक Item को ColorEnumerator() Class के Enumerator Object के रूप में Return करता है:

return new ColorEnumerator(Colors);

परिणामस्वरूप Spectrum Class का Colors नाम का Strings का पूरा Collection एक Parameter के रूप में निम्नानुसार ColorEnumerator() Constructor के माध्‍यम से ColorEnumerator Class में पहुंचता है और ColorEnumerator Class में Defined color नाम के Array Field में Store हो जाता है:

        public ColorEnumerator(string[] theColors) 		// Constructor
        {
            colors = new string[theColors.Length];
            for (int i = 0; i < theColors.Length; i++)
                colors[i] = theColors[i];
        }

चूंकि Spectrum Type में Defined Color नाम का पूरा Collection ColorEnumerator() Constructor के माध्‍यम से ColorEnumerator Class में Defined colors नाम के Strings के Array में Store हो जाता है।

इसलिए IEnumerator Interface के MoveNext() तथा Reset() Methods तथा Current नाम की Property, सभी इस Collection के Data को Directly Access करने में सक्षम हो जाते हैं। परिणामस्वरूप जब Main() Method में निम्नानुसार foreach Loop Run होता है:

            foreach (string color in spectrum)
                Console.WriteLine(color);

तो हर Iteration में colors नाम के Field Array में Stored एक Color का नाम String के रूप में Return होता है और MoveNext() Method Current Property में अगली String का Pointer Set कर देता है तथा One by One Array में Stored सभी Elements Output में Display हो जाते हैं।

C# in Hindiये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook C#.NET in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

C#.NET in Hindi | Page:908 | Format: PDF

BUY NOW GET DEMO REVIEWS