lamda

1. ¶÷´Ù ½Ä


¶÷´Ù½ÄÀº ¹«¸í ¸Þ¼Òµå¸¦ ´Ü¼øÇÑ °è»ê½ÄÀ¸·Î Ç¥ÇöÇÑ°ÍÀÌ´Ù.
¶÷´Ù ½ÄÀº ¶÷´Ù ¿¬»êÀÚ => ¿ÞÂÊ¿¡ ÀÔ·Â ¸Å°³ º¯¼ö¸¦ ÁöÁ¤ÇÏ°í ¿À¸¥ ÂÊ¿¡ ½ÄÀ̳ª ¹® ºí·ÏÀ» »ðÀÔÇÑ´Ù.

(a, b) => a + b

µ¨¸®°ÔÀÌÆ®¸¦ »ç¿ëÇÑ ¹«¸í ¸Þ¼Òµå¸¦ ¶÷´Ù½ÄÀ¸·Î Ç¥ÇöÇÏ¸é ´ÙÀ½°ú °°´Ù.
¶÷´Ù½ÄÀº ¸Å°³º¯¼ö a, bÀÇ Å¸ÀÔµµ »ý·« ÇÒ¼ö ÀÖ´Ù.

delegate int CalcDelegate(int a, int b);
CalcDelegate A;

//¹«¸í ¸Þ¼Òµå
A = delegate( int a, int b)
      {  
            return a + b; 
       };




--->

delegate int CalcDelegate(int a, int b);
CalcDelegate A;

//¶÷´Ù½Ä
A = ( int a, int b) => a+b;

//µ¿ÀÏÇÑ ¶÷´Ù½Ä
A = (a, b) => a+b;

»ç¿ë ¿¹Á¦¸¦ º¸ÀÚ.

    class Test
    {
        delegate int CalcDelegate(int a, int b);
        public Test()
        {
            CalcDelegate add = (a, b) => a + b;
            Console.WriteLine("add={0}", add(3, 7));
        }
    }

2.  ¶÷´Ù ¹®


Áß°ýÈ£ µÑ·¯½Î°í ÇÔ¼öó·³ »ç¿ë ÇÒ¼ö ÀÖ´Â °ÍÀ» ¶÷´Ù ¹®À̶ó°í ÇÑ´Ù.

        delegate int CalcDelegate(int a, int b);
        public Test()
        {
            CalcDelegate add = (a, b) =>
                {
                    int sum = a + b;
                    Console.WriteLine("add={0}", sum);
                    return sum;
                };

            add(3, 7);
        }

3. ºñµ¿±â ¶÷´Ù

??? ¾ÆÁ÷ ¹ÌÀÛ¼º.......

4. Func, Action µ¨¸®°ÔÀÌÆ®


¹Ýȯ°ªÀÌ ÀÖ´Â Func µ¨¸®°ÔÀÌÆ®¿Í ¹Ýȯ°ªÀÌ ¾ø´Â Action µ¨¸®°ÔÀÌÆ®°¡ ¸¸µé¾îÁ® ÀÖ´Ù.
¹«¸í ¸Þ¼Òµå »Ó¸¸ ¾Æ´Ï¶ó ÀÏ¹Ý ¸Þ¼Òµåµµ »ç¿ëÇÒ¼ö ÀÖ´Ù.

¸®ÅÏ°ªÀÌ ¾ø´Â Action ¿¹Á¦¸¦ º¸ÀÚ.

        void Display()
        {
            Console.WriteLine("Display");
        }

        public Test()
        {
            Action a1 = new Action(Display);
            a1();

            Action a11 = this.Display;
            a11();

            Action<string> a2 = (str) => Console.WriteLine("Action<string> {0}", str);
            a2("test");
        }

a1Àº Action ´ë¸®ÀÚ·Î ÀÏ¹Ý ÇÔ¼öÀÎ Display( ) ÇÔ¼ö¸¦ È£ÃâÇÏ°í ÀÖ´Ù.
a11µµ a1°ú °° Action ´ë¸®ÀÚ·Î ÀÏ¹Ý ÇÔ¼öÀÎ Display( ) ÇÔ¼ö¸¦ È£ÃâÇÏ°í ÀÖ´Ù.

a2´Â Action<T> ´ë¸®ÀÚ·Î ¹«¸í ¸Þ¼Òµå¸¦ »ç¿ëÇÏ°í ÀÖ´Ù.
T1ºÎÅÍ T16±îÁö 16°³ÀÇ ¸Å°³º¯¼ö¸¦ Áö¿øÇÑ´Ù.

¸®ÅÏ°ªÀÌ ÀÖ´Â Func ¿¹Á¦¸¦ º¸ÀÚ.

        bool Display()
        {
            Console.WriteLine("Display");
            return true;
        }

        public Test()
        {
            Func<bool> a1 = this.Display;
            a1();

            Func<string, float> a2 = (str) => 0.5f;
            float ret = a2("test");
            Console.WriteLine("a2 = {0}", ret);
        }

a1Àº Func<TResult> ´ë¸®ÀÚ·Î ÀÏ¹Ý ÇÔ¼öÀÎ Display¸¦ ÇÔ¼ö¸¦ È£ÃâÇÏ°í ÀÖ´Ù.

a2´Â Func<T, TResult> ´ë¸®ÀÚ·Î ¹«¸í ¸Þ¼Òµå¸¦ »ç¿ëÇÏ°í ÀÖ´Ù.
T1ºÎÅÍ T16±îÁö 16°³ÀÇ ¸Å°³º¯¼ö¸¦ Áö¿øÇÑ´Ù.

Func<TResult> DelegateÀÇ ½ÇÁ¦ ±¸ÇöÀº ´ÙÀ½°ú °°´Ù.

public delegate TResult Func<out TResult>();

Func<TResult> Delegate´Â ¸Å°³º¯¼ö°¡ ¾ø°í ¸®ÅÏ°ª¸¸ ÀÖ´Ù.
¶÷´Ù¹®À¸·Î Ç¥Çö ÇÒ¶§´Â ÀÏ¹Ý ÇÔ¼öó·³ returnÀ» ºÙÀÌÁö¸¸ ¶÷´Ù½ÄÀ¸·Î ÇÒ¶§´Â returnÀ» ¾È ºÙ¿©µµ µÈ´Ù.

using System;

public void Test()
{
    Func<int> f;
#if false            
    f = () =>
    {
        return (2 + 3);
    };
#else            
    f = () => 2+3;
#endif
    Console.WriteLine(f());
}

5. ¶÷´Ù º¯¼ö ĸÃÄ

¶÷´Ù¹®(½Ä)¿¡ »ç¿ëµÇ´Â º¯¼ö´Â ÄÄÆÄÀϽà º°µµ·Î ¸¸µé¾î ÁØ´Ù. ÁÖÀÇÁ¡À» »ìÆ캸ÀÚ.

using System;
using System.Windows.Forms;

public class Name
{
    private string instanceName;

    public Name(string name)
    {
        this.instanceName = name;
    }

    public void DisplayToWindow()
    {
        MessageBox.Show(this.instanceName);
    }
}

public class LambdaExpression
{
    public static void Main()
    {
        Name testName = new Name("Koani");
        Action showMethod = () => testName.DisplayToWindow();
        showMethod();
    }
}

Action showMethod = () => testName.DisplayToWindow();

À§ÀÇ ¶÷´Ù¹®Àº ÄÄÆÄÀϽÿ¡ ´ÙÀ½°ú °°ÀÌ »ý¼ºµÈ´Ù.

public class [ÀÓ½ÃŬ·¡½º]
{
    Name _name;

    public void _f()
    {
        _name.DisplayToWindow(); // showMethod¿¡ ³Ö¾ú´ø Lambda ¸Þ¼­µå body
    }
}

¿ø·¡ÀÇ ¼Ò½º¸¦ ´ÙÀ½°ú °°ÀÌ ¹Ù²Û´Ù.

public static void Main()
{
    [ÀÓ½ÃŬ·¡½º] _var = new [ÀÓ½ÃŬ·¡½º]();

    _var._name = new Name("Koani");
    Action showMethod = _var._f;

    showMethod();
}

¶÷´ÙÀÇ º¯¼ö ĸÃÄ¿¡ ´ëÇÑ ÁÖÀÇ »çÇ×À¸·Î ´ÙÀ½ ¿¹Á¦¸¦ º¸ÀÚ.

//http://stackoverflow.com/questions/451779/how-to-tell-a-lambda-function-to-capture-a-copy-instead-of-a-reference-in-c

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<Action> actions = new List<Action>();

        for (int i = 0; i < 10; ++i)
        {
            actions.Add(() => Console.WriteLine(i));
        }

        foreach (Action a in actions)
        {
            a();
        }

        // ±â´ëÇÏ´ø Ãâ·Â: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
        // ½ÇÁ¦ Ãâ·Â:    10, 10, 10, 10, 10, 10, 10, 10, 10, 10
    }
}

Àӽà Ŭ·¡½º°¡ ¸¸µé¾îÁö°í ¸¶Áö¸· °ª 10À» °øÀ¯ÇÏ°Ô µÈ´Ù.

//http://stackoverflow.com/questions/451779/how-to-tell-a-lambda-function-to-capture-a-copy-instead-of-a-reference-in-c

using System;
using System.Collections.Generic;

public class [ÀÓ½ÃŬ·¡½º]
{
    public int _i;
    public void _f()
    {
        Console.WriteLine(_i);
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Action> actions = new List<Action>();

        [ÀÓ½ÃŬ·¡½º] _var = new [ÀÓ½ÃŬ·¡½º]();

        for (_var._i = 0; _var._i < 10; ++_var._i)
        {
            actions.Add(_var._f);
        }

        foreach (Action a in actions)
        {
            a();
        }
    }
}

1, 2, 3, 4, 5, 6, 7, 8, 9, 10ÀÌ Á¤»óÀûÀ¸·Î Ãâ·Â µÇ°Ô ÇÒ·Á¸é ´ÙÀ½°ú °°ÀÌ ÇÑ´Ù.
for ±¸¹® ¿µ¿ª¾È¿¡ º¯¼ö¸¦ Çϳª ¼±¾ðÇؼ­ °ªÀ» Àü´ÞÇÑ´Ù.

using System;
using System.Collections.Generic;
class Program
{
    static void Main(string[] args)
    {
        List<Action> actions = new List<Action>();

        for (int i = 0; i < 10; ++i)
        {
            int v = i;
            actions.Add(() => Console.WriteLine(v));
        }

        foreach (Action a in actions)
        {
            a();
        }
    }
}

ÄÄÆÄÀϽÿ¡ »ý¼ºµÈ ÄÚµå´Â ´ÙÀ½°ú °°´Ù.
for ±¸¹® ¾È¿¡¼­ º¯¼ö¸¦ ¸¸µé¾ú±â ¶§¹®¿¡ for ¾È¿¡¼­ new°¡ »ý¼ºµÇ¾ú´Ù.

//http://stackoverflow.com/questions/451779/how-to-tell-a-lambda-function-to-capture-a-copy-instead-of-a-reference-in-c

using System;
using System.Collections.Generic;

public class [ÀÓ½ÃŬ·¡½º]
{
    public int _v;
    public void _f()
    {
        Console.WriteLine(_i);
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Action> actions = new List<Action>();

        for (int i = 0; i < 10; ++i)
        {
            [ÀÓ½ÃŬ·¡½º] _var = new [ÀÓ½ÃŬ·¡½º]();
            _var._v = i;
            actions.Add(_var._f);
        }

        foreach (Action a in actions)
        {
            a();
        }
    }
}


ÂüÁ¶)
http://mrw0119.tistory.com/22
https://msdn.microsoft.com/ko-kr/library/bb397687.aspx
http://www.sysnet.pe.kr/Default.aspx?mode=2&sub=0&detail=1&wid=10817