zoukankan      html  css  js  c++  java
  • 委托-系统

    泛型委托

            public delegate T1 Method<T1, T2>(T1 a);
            static void Main(string[] args)
            {
                Method<string, int> method = (string a) => { return a; };
                Console.WriteLine(method("有参数返回"));
                Console.ReadLine();
            }
    View Code
            public delegate T1 Method<T1, T2>(T1 a);
            static void Main(string[] args)
            {
                Method<int, int> method = (int a) => { return a*a*a; };
                Console.WriteLine(method(10));
                Console.ReadLine();
            }
    View Code

    委托作为参数传递

    namespace 委托
    {
        class Program
        {
            public delegate T Method<T>(T a,T b);
            static void Main(string[] args)
            {
                int result = 0;
                //制作将委托作为参数的方法
                int Test(Method<int> method, int a,int b)
                {
                    result = method(a,b);
                    return result;
                }
                //之后要传入的方法
                int Add(int a,int b)
                {
                    return a + b;
                }
                //调用
                Console.WriteLine(Test(Add,10,20));
    
                Console.ReadLine();
            }
    
        }
    }
    View Code

    系统委托

    namespace 委托
    {
        class Program
        {
            static void Main(string[] args)
            {
                int result = 0;
                //制作将委托作为参数的方法
                //Func最后一个参数是返回值
                int Test(Func<int,int,int> func,int a,int b)
                {
                    result = func(a,b);
                    return result;
                }
                //之后要传入的方法
                int Add(int a,int b)
                {
                    return a + b;
                }
                //调用
                Console.WriteLine(Test(Add,10,20));
    
                Console.ReadLine();
            }
    
        }
    }
    View Code
    namespace 委托
    {
        class Program
        {
            static void Main(string[] args)
            {
                //制作将委托作为参数的方法
                //Action是没有返回值的
                void Test(Action<int,int> func,int a,int b)
                {
                    func(10,20);
                }
                //之后要传入的方法
                void Add(int a,int b)
                {
                    Console.WriteLine(a + b); 
                }
                //调用
                Test(Add,10,20);
    
                Console.ReadLine();
            }
    
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Action<int> action1 = (a) => { Console.WriteLine(a); };
                Action<string> action2 = new Action<string>((a) => { Console.WriteLine(a); });
                Action<int> action3 = new Action<int>(a => { Console.WriteLine(a); });
                action1(10);
                action2("10");
                action3(10);
                Console.ReadLine();
            }
    
        }
    }
    View Code
  • 相关阅读:
    合并两个排序的链表
    反转链表
    java网络编程之TCP通讯
    java网络编程之UDP通讯
    Java中的线程同步机制
    阿里研发工程师面试题三个小结
    Android开发的进阶之路
    获取一个字符串中每一个字母出现的次数使用map集合
    Android常见面试题目
    Java垃圾回收
  • 原文地址:https://www.cnblogs.com/Luck1996/p/11983899.html
Copyright © 2011-2022 走看看