๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90

 

 

 

foreach

foreach ๋ฌธ์€ for ๋ฌธ์ฒ˜๋Ÿผ ์ธ๋ฑ์Šค ๋ณ€์ˆ˜๊ฐ€ ํ•„์š” ์—†๋‹ค. ๋ฐฐ์—ด์ด๋‚˜ ๋ฆฌ์ŠคํŠธ๊ฐ™์€ ์ปฌ๋ ‰์…˜์—์„œ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

foreach ๋ฌธ์ด ๊ฐ์ฒด ๋‚ด์˜ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” foreach ๋ฌธ๊ณผ์˜ ์•ฝ์†์„ ์ง€์ผœ์•ผ๋งŒ ํ•œ๋‹ค.

๊ทธ ์•ฝ์†์€ IEnumerable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” ํด๋ž˜์Šค ๊ตฌํ˜„์ด๋‹ค.

 

์ฆ‰, ์–ด๋–ค ํด๋ž˜์Šค๋ผ๋„ IEnumerable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด foreach ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์–˜๊ธฐ๊ฐ€ ๋œ๋‹ค.

 

 

 

 

 

IEnumerable ์ธํ„ฐํŽ˜์ด์Šค

์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋Š” ๋ฐ˜๋“œ์‹œ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ชจ๋“  ๋‚ด์šฉ์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๊ธฐ์–ตํ•  ๊ฒƒ์ด๋‹ค.

IEnumerable ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋Š” ๋‹จ ํ•˜๋‚˜ ๋ฟ์ด๋ฉฐ, ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋Š” ๋‹ค์Œ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

IEumerator GetEnumerator();     // IEnumerator ํ˜•์‹์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜

 

GetEnumerator() ๋ฉ”์†Œ๋“œ๋Š”IEnumerator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.

์ง์ ‘ IEnumerator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•ด๋„ ๋˜๊ณ , ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ง€์›ํ•˜๋Š” yield ๋ฌธ์„ ์ด์šฉํ•ด๋„ ๋œ๋‹ค.

๋จผ์ €, yield ๋ฌธ์„ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

 

 

 

 

 

yield ๋ฌธ

yield ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด IEnumerator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„, ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.

 

yield return

ํ˜„์žฌ ๋ฉ”์†Œ๋“œ(GetEnumerator())์˜ ์‹คํ–‰์„ ์ผ์‹œ ์ •์ง€ํ•˜๊ณ , ํ˜ธ์ถœ์ž์—๊ฒŒ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋ฉ”์†Œ๋“œ๊ฐ€ ๋‹ค์‹œ ํ˜ธ์ถœ๋˜๋ฉด, ์ผ์‹œ ์ •์ง€๋œ ์‹คํ–‰์„ ๋ณต๊ตฌํ•˜์—ฌ yield return ๋˜๋Š” yield break ๋ฌธ์„ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ์ž‘์—…์„ ๊ณ„์†ํ•œ๋‹ค.

using System;
using System.Collections;

class MyEnumerator
{
     private int[] numbers = { 1, 2, 3, 4 };
        
     public IEnumerator GetEnumerator()
     {
         yield return numbers[0];
         yield return numbers[1];
         yield return numbers[2];
         yield break;             // yield break๋Š” GetEnumerator() ๋ฉ”์†Œ๋“œ๋ฅผ ์ข…๋ฃŒ์‹œํ‚ด
         yield return numbers[3]; // ๋”ฐ๋ผ์„œ ์ด ๋ถ€๋ถ„์€ ์‹คํ–‰๋˜์ง€ ์•Š์Œ
     }
}

 

ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ์‹คํ–‰ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

var _object = new MyEnumerator();
           
foreach(int i in _object)
{
    Console.WriteLine(i);
}

 

 

yield ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด IEnumerator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋ฅผ ๋ณ„๋„๋กœ ๊ตฌํ˜„ํ•  ํ•„์š”์—†์ด, ์ด๋ ‡๊ฒŒ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, ์ง์ ‘ IEnumerator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ณธ๋‹ค๋ฉด ์–ด๋–จ๊นŒ?

 

 

 

 

 

IEnumerator ์ธํ„ฐํŽ˜์ด์Šค

interface IEnumerator
{
    boolean MoveNext();
    void Reset();
    Object Current { get;}
}
  • MoveNext()  :  ๋‹ค์Œ ์š”์†Œ๋กœ ์ด๋™. ์ปฌ๋ ‰์…˜์˜ ๋์„ ์ง€๋‚œ ๊ฒฝ์šฐ์—๋Š” False, ์ด๋™์ด ์„ฑ๊ณตํ•œ ๊ฒฝ์šฐ์—๋Š” True ๋ฆฌํ„ด
  • Current  :  ์ปฌ๋ ‰์…˜์˜ ํ˜„์žฌ ์š”์†Œ ๋ฆฌํ„ด
  • Reset()   :  ์ปฌ๋ ‰์…˜์˜ ์ฒซ ๋ฒˆ์งธ ์œ„์น˜ "์•ž"์œผ๋กœ ์ด๋™
    • ์ฒซ ๋ฒˆ์งธ ์œ„์น˜๊ฐ€ 0๋ฒˆ์ธ ๊ฒฝ์šฐ, Reset() ํ˜ธ์ถœ ์‹œ -1 ๋ฒˆ์œผ๋กœ ์ด๋™.
    • ์ฒซ ๋ฒˆ์งธ ์œ„์น˜๋กœ์˜ ์ด๋™์€ MoveNext()๋ฅผ ํ˜ธ์ถœํ•œ ๋‹ค์Œ์— ์ด๋ฃจ์–ด์ง  

 

์˜ˆ์ œ ์ฝ”๋“œ

using System;
using System.Collections;

namespace Enumerable
{

   class MyList : IEnumerable, IEnumerator
   {
        private int[] array;
        int position = -1;               // ์ปฌ๋ ‰์…˜์˜ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ณ€์ˆ˜
        
        public MyList()
        {
            array = new int[3];
        }
        
        public int this[int index]      // ์ธ๋ฑ์„œ
        {
            get { return array[index]; }
            set
            {
                if (index >= array.Length)
                {
                    Array.Resize<int>(ref array, index + 1);
                    Console.WriteLine($"Array Resized : {array.Length}");
                }
                
                array[index] = value;
            }
        }
        
        
        // IEnumerator ๋ฉค๋ฒ„
        public object Current
        {
            get { return array[position]; }
        }
        
        
        // IEnumerator ๋ฉค๋ฒ„
        public bool MoveNext()
        {
            if (position == array.Length - 1)
            {
                Reset();
                return false;
            }
            
            position++;
            return (position < array.Length);
        }
        
        
        // IEnumerator ๋ฉค๋ฒ„
        public void Reset()
        {
            position = -1;
        }
        
        // IEnumerable ๋ฉค๋ฒ„
        public IEnumerator GetEnumerator()
        {
            return this;
        }
   }
}

 

๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ๋Œ๋ ค๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜จ๋‹ค.

MyList list = new MyList();
for(int i = 0; i < 5; i++)
{
    list[i] = i;
}

foreach(int i in list)
{
    Console.WriteLine(i);
}

 

 

 

 

  • ์ด ๊ธ€์€ <์ด๊ฒƒ์ด C#์ด๋‹ค> ์ฑ…์„ ๋ฐ”ํƒ•์œผ๋กœ ๊ณต๋ถ€ํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•