02 June 2016

Lambda in C#

Biểu thức Lambda trong C#
Lambda C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListDelegate
{

    class Program
    {
        public delegate int myDelegate(int a, int b);

        static void Main(string[] args)
        {
            myDelegate myClass = (a, b) => a + b;
            Console.WriteLine("Result: "+myClass(4,4));
            Console.Read();
        }
    }
}



Biểu thức Lambda trong C#
Lambda C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo
{

    class Program
    {
        delegate int del(int a);
        static void Main(string[] args)
        {
            del myDel = x => x * x; //Lambda
            
            Console.WriteLine(myDel(5));
            Console.Read();
        }
    }
}


Biểu thức Lambda trong C#
Lambda C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demoDelegate
{

    class Program
    {
        static void Main(string[] args)
        {
            int[] source = new[] { 2, 3, 4, 6, 7, 8, 9 };

            foreach (int i in source.Where(
                x =>
                {
                    if (x < 3)
                        return true;
                    else if (x >= 7)
                        return true;
                    return false;
                }))
                Console.WriteLine(i);
            Console.Read();
        }
    }
}


Biểu thức Lambda trong C#
Lambda C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo
{

    class Program
    {
        static void Main(string[] args)
        {
            Func<int, int> func1 = x => x + 1;
            Func<int, int> func2 = x => { return x + 1; };
            Func<int, int> func3 = (int x) => x+1;
            Func<int, int> func4 = (int x) => { return x + 1; };
            Func<int,int, int> func5 =(x,y) => x * y;
            Func<int, int> func6 = delegate(int x) {return x + 1;};
            Func<int> func7 = delegate { return 4 + 1; };
            Action func8 = () => Console.WriteLine("Func8 Action" );

            Console.WriteLine(func1.Invoke(1));
            Console.WriteLine(func2.Invoke(1));
            Console.WriteLine(func3.Invoke(1));
            Console.WriteLine(func4.Invoke(1));
            Console.WriteLine(func5.Invoke(1,1));
            Console.WriteLine(func6.Invoke(1));
            Console.WriteLine(func7.Invoke());
            func8.Invoke();

            Console.Read();
        }
    }
}









0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang