System.Array – Pre Implemented

System.Array: IEnumerableIEnumerator Interface के Methods को Define किए बिना भी हम एक अन्‍य तरीके का प्रयोग करके अपनी किसी Collection Class के लिए इन Methods के पहले से Implemented Version को Use कर सकते हैं।

चूंकि हम जानते हैं कि System.Array भी .NET Framework का एक Reference Type है और इस Type में भी इन Methods को Implement किया गया है, जिसकी वजह से Array Type के Objects के लिए foreach Looping को Use किया जाना सम्भव हो पाता है।

इसलिए यदि हम System.Array में Implemented इन Methods को हमारी WatchArray Class के लिए Delegate कर सकते हैं। यानी C# Compiler हमारे WatchArray Class के Array के लिए System.Array Class में Implemented इन Methods को Use कर लेगा और हमें हमारे Class के Objects के Array के लिए foreach Looping को Use करने की सुविधा Provide कर देगा।

क्योंकि हमने “Base Class Method as Interface Implementation” Section में देखा था कि यदि किसी Interface में कोई Method Declare किया गया हो और किसी Derived Class को जिस Base Class से Derive किया जा रहा हो, उस Base Class में उस Interface के Methods को Implement किया गया हो, तो Derived Class के लिए Base Class का Implemented Version Use हो जाता है।

साथ ही हम ये भी जानते हैं कि Inheritance भी दो प्रकार के होते हैं, जिन्हें “is-a” Relationship व “has-a” Relationship द्वारा Represent किया जा सकता है। इसलिए जब हम किसी Class में System.Array Type का Object Create करते हैं, तो वास्तव में हम एक प्रकार का “has-a” Inheritance कर रहे होते हैं, क्योंकि Current Class में System.Array Composed या Contained हो जाता है।

इस तरह से “has-a” Inheritance व “Base Class Method as Interface Implementation for Derived Class”, दोनों के Concepts को Combine करके हम निम्नानुसार तरीके से भी अपनी WatchArray Class के लिए foreach Looping Construct को Define कर सकते हैं:

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

namespace CSharpInterface
{
    public class Watch
    {
        internal byte hour, minute;
        internal string name;
    }

    class WatchArray : IEnumerable
    {
        Watch[] RoyalWatches = new Watch[4];

        public WatchArray()
        {
            RoyalWatches[0] = new Watch() { hour = 12, minute = 0, name = "HMT" };
            RoyalWatches[1] = new Watch() { hour = 10, minute = 10, name = "SONATA" };
            RoyalWatches[2] = new Watch() { hour = 5, minute = 40, name = "RADO" };
            RoyalWatches[3] = new Watch() { hour = 3, minute = 0, name = "TITAN" };
        }

        public IEnumerator GetEnumerator()
        {
            // Return the array object's IEnumerator.
            return RoyalWatches.GetEnumerator();
        }
    }

    class UsingWatchArray
    {
        public static void Main(string[] args)
        {
            WatchArray Watches = new WatchArray();

            foreach (Watch x in Watches)
            {
                Console.WriteLine("Time of {0} - {1}:{2}", x.name, x.hour, x.minute);
            }
        }
    }
}

// Output:
   Time of HMT - 12:0
   Time of SONATA - 10:10
   Time of RADO - 5:40
   Time of TITAN - 3:0

जैसाकि इस बार इस Program के Output से हम समझ सकते हैं कि अब foreach Statement WatchArray Class के Objects के Array के लिए Normal तरीके से काम कर रहा है, क्योंकि हमने WatchArray Class में सबसे पहले निम्नानुसार Statement द्वारा IEnumerable Interface को Implement किया है:

    class WatchArray : IEnumerable

और फिर निम्नानुसार Code द्वारा:

        public IEnumerator GetEnumerator()
        {
            // Return the array object's IEnumerator.
            return RoyalWatches.GetEnumerator();
        }

Composition तकनीक का प्रयोग करके System.Array Class में Implemented GetEnumerator() Method को ही Interface द्वारा Reuse करने के लिए Code कर दिया है। साथ ही हमने GetEnumerator() Method को public Access Mode में Define किया है, ताकि किसी भी External Class में WatchArray Type के Object के लिए foreach Statement को Use किया जा सके। जबकि यदि हम उपरोक्त Code को निम्नानुसार Explicit Interface Member Implementation तरीके का प्रयोग करके Define कर दें-

        IEnumerator IEnumerable.GetEnumerator()
        {
            // Return the array object's IEnumerator.
            return RoyalWatches.GetEnumerator();
        }

तो किसी भी Class में foreach Construct का प्रयोग करके तो WatchArray Class के Object के विभिन्न Members को Access कर सकते हैं, लेकिन WatchArray के GetEnumerator() Method को किसी भी Class में Directly Access नहीं कर सकते, क्योंकि Explicit Interface Member Implementation किसी भी External Class के लिए पूरी तरह से Private हो जाता है।

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

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

BUY NOW GET DEMO REVIEWS