02 June 2016

Delegate in C#

Delegate Hàm có tên - 2 class
Delegate C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListDelegate
{
    class DelegateClass
    {
        public delegate int myDelegate(int a, int b);

        public int tinhTong(int a, int b) // hàm có tên
        {
            return a + b;
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            DelegateClass myClass = new DelegateClass();

            DelegateClass.myDelegate objSum = null;

            objSum = myClass.tinhTong;

            if (myClass != null)
            {
            Console.WriteLine("Result: "+objSum(4,4));
            Console.Read();
            }
        }
    }
}

Delegate Hàm không có tên - 2 class
Delegate Anonymous method C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListDelegate
{
    class DelegateClass
    {
        public delegate int myDelegeate(int a, int b);        
    }
    class Program
    {
        static void Main(string[] args)
        {
            DelegateClass myClass = new DelegateClass();

            DelegateClass.myDelegeate objSum = null;

            objSum = delegate(int a, int b) //hàm ko co tên
            {
                return a + b;
            };

            if(objSum != null){
                Console.WriteLine("Sum fo two interger is: "+objSum(4,4));
            }
            Console.Read();
        }
    }
}


Delegate Hàm có tên - trong 1 class
Delegate C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1

{

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

        public int tinhtong(int a, int b)
        {
            return a + b;
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            myDelegate myClass = new myDelegate(p.tinhtong);
            Console.WriteLine(myClass(4,4));
            Console.Read();
        }
    }
}

Delegate không có tên - trong 1 class
Delegate C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        static void Main(string[] args)
        {
            myDelegate obj = null;
            obj = delegate(int a, int b)
            {
                return a + b;
            };
            if(obj != null){
            Console.WriteLine(obj(4,4));
            Console.Read();
            }
        }
    }
}


Delegate in action - trong 1 class
Delegate 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);
        public int add(int a, int b)
        {
            return a + b;
        }
        static void Main(string[] args)
        {

            Program p = new Program(); 
            myDelegate myClass = new myDelegate(p.add);

            Console.WriteLine("Result: "+p.Calculate(myClass));
            Console.Read();
        }
        //Hạn chê sử dụng static nên không dùng public static int Calculate
        public int Calculate(myDelegate myClass)
        {
            int r = myClass(4,4);
            return r;
        }

    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang