Enumerator

ÄÝ·º¼ÇÀ» ¿­°ÅÀÚ¸¦ ÅëÇÏ¿© Á¢±ÙÇÏ´Â ¹æ¹ý¿¡ ´ëÇؼ­ ¾Ë¾Æº¸ÀÚ.

IEnumerator


IEnumerator¸¦ »ç¿ëÇϱâ À§ÇØ ´ÙÀ½ ¶óÀÎÀ» Ãß°¡ÇÑ´Ù.

using System.Collections;

MoveNext : ¿­°ÅÀÚ¸¦ Ä÷º¼ÇÀÇ ´ÙÀ½ ¿ä¼Ò·Î À̵¿ÇÑ´Ù.
Reset : Ä÷º¼ÇÀÇ Ã¹ ¹ø° ¿ä¼Ò ¾ÕÀÇ Ãʱâ À§Ä¡¿¡ ¿­°ÅÀÚ¸¦ ¼³Á¤ÇÑ´Ù.

ÃÖÃÊÀÇ À§Ä¡´Â Ä÷º¼ÇÀÇ Ã¹ ¹ø° ¿ä¼Ò ¾ÕÀÌ´Ù.

½ºÆ®¸µ¿¡´Â IEnumerable ÀÎÅÍÆäÀ̽º°¡ ±¸ÇöµÇ¾îÀÖ´Ù.

using System;
using System.Collections;

namespace csharpTest
{
    class Test
    {
        public Test()
        {
            string[] strList = { "one", "two", "three", "four", "five" };

            IEnumerator e = strList.GetEnumerator();

            while (e.MoveNext())
            {
                Console.WriteLine(e.Current);
            }

            Console.WriteLine("--------------------------");

            e.Reset();
            e.MoveNext();
            Console.WriteLine(e.Current);       
        }
    }
}

°á°ú
one
two
three
four
five
--------------------------
one

IEnumerable


IEnumerableÀ» »ç¿ëÇϱâ À§ÇØ ´ÙÀ½ ¶óÀÎÀ» Ãß°¡ÇÑ´Ù.

using System.Collections.Generic;

Á¦³×¸¯ÀÌ ¾Æ´Ñ Ä÷º¼Ç¿¡¼­ ´Ü¼øÇÏ°Ô ¹Ýº¹ÇÒ ¼ö ÀÖµµ·Ï Áö¿øÇÏ´Â ¿­°ÅÀÚ¸¦ ³ëÃâÇÑ´Ù.

using System;
using System.Collections.Generic;

namespace csharpTest
{
    class People
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    class Test
    {
        public Test()
        {
            IEnumerable<People> peopleList = new People[]
            {
                new People { Name = "kim", Age = 20},
                new People { Name = "lee", Age = 20},
                new People { Name = "park", Age = 15},
                new People { Name = "wang", Age = 15}
            };

            foreach (var value in peopleList)
            {
                Console.WriteLine(value.Name + ", " + value.Age);
            }
        }
    }
}

°á°ú

kim, 20
lee, 20
park, 15
wang, 15


IEnumerable¿¡¼­ Äõ¸® ÁúÀÇ


IEnumerable ¸®½ºÆ®¸¦ ÀÌ¿ëÇÏ¿© Sql ¹®Ã³·³ ÁúÀÇ ÇÒ¼ö ÀÖ´Ù.
C#¿¡¼­´Â Linq ¹®À̶ó°í ÇÑ´Ù.

Linq¸¦ »ç¿ëÇÒ·Á¸é ´ÙÀ½ ¶óÀÎÀ» Ãß°¡ÇÑ´Ù.

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

SQL Query¹®°ú Linq¹®Àº ºñ½ÁÇÏ´Ù.

SQL Query¹®
select (column)  from (table) where (Á¶°Ç)

Linq Query¹®
from (¸®½ºÆ®¾ÆÀÌÅÛ) in (¸®½ºÆ®) where Á¶°Ç select (¸®ÅÏ°ª)

¿¹Á¦1) ÄÚµå
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;

namespace csharpTest
{
    class People
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    class Test
    {
        public Test()
        {
            IEnumerable<People> peopleList = new People[]
            {
                new People { Name = "kim", Age = 20},
                new People { Name = "lee", Age = 20},
                new People { Name = "park", Age = 15},
                new People { Name = "wang", Age = 15}
            };

            var query = from p in peopleList
                        where p.Age == 20
                        select p;

            foreach (var value in query)
            {
                Console.WriteLine(value.Name + ", " + value.Age);
            }
        }
    }
}

¿¹Á¦1) °á°ú
kim, 20
lee, 20

"select p" ´ë½Å "select p.Name"À» »ç¿ëÇϸé Name ¹®ÀÚ¿­À» query¿¡ ¸®ÅÏÇÑ´Ù.

¿¹Á¦2) ÄÚµå
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;

namespace csharpTest
{

    class Test
    {
        public Test()
        {
            string[] strList = { "one", "two", "three", "four", "five" };

            //IEnumerable<string> enumerable = strList.Where(str => str.Length <= 3);
            IEnumerable<string> enumerable = from str in strList where str.Length <= 3 select str;


            IEnumerator<string> e = enumerable.GetEnumerator();

            while (e.MoveNext())
            {
                Console.WriteLine(e.Current);
            }

        }
    }
}

¿¹Á¦2) °á°ú
one
two


Enumerable.Range ¸Þ¼Òµå

ÁöÁ¤µÈ ¹üÀ§³»ÀÇ Á¤¼ö ½ÃÄö½º¸¦ »ý¼ºÇÑ´Ù.

IEnumerable<int> squares = Enumerable.Range(1, 10)
1ºÎÅÍ 10±îÁö squares¿¡ ä¿î´Ù

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

IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x);

foreach (int num in squares)
{
    Console.WriteLine(num);
}

Select(x => x * x) ¹®À» ÀÌ¿ëÇÏ¿© 1, 4, 9, 16, 25, 36, 49, 64, 81, 100À» squares¿¡ ä¿ì°í ÀÖ´Ù.

Enumerable.Range °ªÀ» ¹è¿­·Î º¯È¯ÇÏ¿© ³ÖÀ»¼öµµ ÀÖ´Ù.

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

//int[] squares = Enumerable.Range(1, 10).ToArray<int>();

var squares = Enumerable.Range(1, 10).ToArray();
            
foreach (int num in squares)
{
    Console.WriteLine(num);
}


ÂüÁ¶)
IEnumerable  »ç¿ë¹ý
http://www.dotnetperls.com/ienumerable

IEnumerable  ÀÎÅÍÆäÀ̽º
https://msdn.microsoft.com/ko-kr/library/system.collections.ienumerable(v=vs.110).aspx

IEnumerable(Linq) ¿¹Á¦
http://j07051.tistory.com/577

Linq] C# Linq - ±âº» ±¸¹®   SQL ¹®
http://herbyoung.tistory.com/45