My Programming Notes

Here are Notes.... come from debugging

My Programming Notes

Here are Notes.... come from debugging

۱ مطلب با کلمه‌ی کلیدی «IEnumerator» ثبت شده است

چهارشنبه, ۲۱ تیر ۱۳۹۶، ۰۴:۵۰ ب.ظ

تفاوت بین Interface های Enumerator و Enumerable در C# - قسمت دوم

 

-- ادامه پست قبل

  مثال های از استفاده از IEnumerable ,  IEnumerator Interface:

        }ngth; i++)

}

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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);
        }
    }

 خروجی :

violet

blue

cyan

green

yellow

orange

red

  

Generic Enumeration Interfaces  :

ما تا الان در مورد Non Generic Enumeration Interfaces   بحث کردیم ولی در واقعیت ما بیشتر از نوع مطمئن Generic Enumeration Interfaces  استفاده می کنیم . که آنها خود IEnumerable  و IEnumerator interfaces. دارند.

مهمترین تفاوت NonGeneric و Generic  موارد زیر هستند:

       در  the NonGeneric Interface form  :

متد GetEnumerator     از اینترفیس  IEnumerable نمونه ای  از کلاس enumerator  را بر می گرداند که IEnumerator را پیاده سازی می کند.

کلاسی که IEnumerator  را پیاده سازی می کند همچنین خصوصیت  Current  را  پیاده سازی می کند که  refrence ای از نوع object بر می گرداند. که در نهایت باید به نوع واقعی شی تبدیل شود.

  در  the Generic Interface form    :

متد GetEnumerator   از اینترفیس  IEnumerable نمونه ای  از کلاس IEnumerator را برمی پرداند.

کلاسی که IEnumerator را پیاده سازی می کند همچنین خصوصیت Current را هم پیاده سازی می کند که یک مقدا رواقعی را نمونه سازی میکند به جای  reference  به شی کلاس پایه.

 


۰ نظر موافقین ۰ مخالفین ۰ ۲۱ تیر ۹۶ ، ۱۶:۵۰
سارا زکی زاده