C#/์ด๊ฒƒ์ด C#์ด๋‹ค

59. foreach๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ผ๋ฐ˜ํ™” ํด๋ž˜์Šค

Rainbow๐ŸŒˆCoder 2022. 2. 28. 18:19
728x90

foreach๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ํ•„์š”ํ•œ ๊ฒƒ์€...

IEnumerable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•ด์„œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค.

 

 

์ผ๋ฐ˜ํ™” ํด๋ž˜์Šค๋„ IEnumerable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•˜๋ฉด ์ผ๋‹จ์€ foreach๋ฅผ ํ†ตํ•ด ์ˆœํšŒํ•  ์ˆ˜ ์žˆ์ง€๋งŒ,

์š”์†Œ๋ฅผ ์ˆœํšŒํ•  ๋•Œ๋งˆ๋‹ค ํ˜•์‹ ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์˜ค๋ฒ„๋กœ๋“œ๊ฐ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค.

(๊ธฐ๊ป ์ผ๋ฐ˜ํ™”๋ฅผ ํ†ตํ•ด ํ˜•์‹ ๋ณ€ํ™˜์„ ์ œ๊ฑฐํ–ˆ๋”๋‹ˆ foreach ๊ตฌ๋ฌธ์—์„œ ํ˜•์‹ ๋ณ€ํ™˜์„ ์ผ์œผ์ผœ ์„ฑ๋Šฅ์„ ์ €ํ•˜์‹œํ‚จ๋‹ค๋‹ˆ...)

 

System.Collections.Generic ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์ด ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋Š” ์—ด์‡ ๊ฐ€ ์žˆ๋‹ค.

IEnumerable์˜ ์ผ๋ฐ˜ํ™” ๋ฒ„์ „์ธ IEnumerable<T> ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋ฐ”๋กœ ๊ทธ๊ฒƒ์ด๋‹ค.

์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•˜๋ฉด ํ˜•์‹ ๋ณ€ํ™˜์œผ๋กœ ์ธํ•œ ์„ฑ๋Šฅ ์ €ํ•˜๊ฐ€ ์—†์œผ๋ฉด์„œ๋„

foreach ์ˆœํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•œ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋‹ค์Œ์€ IEnumerable<T> ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ฉ”์†Œ๋“œ์ด๋‹ค.

๋ฉ”์†Œ๋“œ ์„ค๋ช…
IEnumerator GetEnumerator() IEnumerator ํ˜•์‹์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜(IEnumerable๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์€ ๋ฉ”์†Œ๋“œ)
IEnumerator<T> GetEnumerator() IEnumerator<T> ํ˜•์‹์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜

 

 

<์˜ˆ์ œ>

using System;
using System.Collections;
using System.Collections.Generic;

namespace EnumerableGeneric
{
    class MainApp
    {
        class MyList<T> : IEnumerable<T>, IEnumerator<T>
        {
            private T[] array;
            int position = -1;

            public MyList()
            {
                array = new T[3];
            }

            public T this[int index]
            {
                get { return array[index]; }
                set
                {
                    if (index >= array.Length)
                    {
                        Array.Resize<T>(ref array, index + 1);
                        Console.WriteLine($"Array Resized : {array.Length}");
                    }
                    array[index] = value;
                }
            }
            public int Length
            {
                get { return array.Length; }
            }

            public IEnumerator<T> GetEnumerator()
            {
                return this;
            }
            IEnumerator IEnumerable.GetEnumerator()
            {
                return this;
            }
            public T Current
            {
                get { return array[position]; }
            }
            object IEnumerator.Current
            {
                get { return array[position]; }
            }
            public bool MoveNext()
            {
                if(position==array.Length-1)
                {
                    Reset();
                    return false;
                }
                position++;
                return (position<array.Length);
            }
            public void Reset()
            {
                position = -1;
            }
            public void Dispose()
            {

            }
        }

        static void Main(string[] args)
        {
            MyList<string> strList = new MyList<string>();
            strList[0] = "abc";
            strList[1] = "def";
            strList[2] = "ghi";
            strList[3] = "jkl";
            strList[4] = "mno";

            foreach (var s in strList) Console.WriteLine(s);

            Console.WriteLine();
            MyList<int> intList = new MyList<int>();
            intList[0] = 0;
            intList[1] = 1;
            intList[2] = 2;
            intList[3] = 3;
            intList[4] = 4;

            foreach (var i in intList) Console.WriteLine(i);

            Console.WriteLine();

        }
    }
}

<์˜ˆ์ œ ์ถœ๋ ฅ>

Array Resized : 4
Array Resized : 5
abc
def
ghi
jkl
mno

Array Resized : 4
Array Resized : 5
0
1
2
3
4
728x90