C# 委托 泛型

一、Action

using System;


namespace ClassAndIn
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Action action = Say;
            action();
            Action<int> action1 = Cal;
            action1(6);
        }

        static void Say()
        {
            Console.WriteLine("Hello, World");
        }

        static void Cal(int x)
        {
            Console.WriteLine(x * 100);
        }
    }
}

二、Func

using System;


namespace ClassAndIn
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Func<int, int, int> func1 = Add;
            Console.WriteLine(func1(10, 20));

            Func<double, double, double> func2 = Add;
            Console.WriteLine(func2(20.5, 21.5));
            Console.WriteLine(func2(20.5, 21.5).GetType().Name);
        }

        static int Add(int x, int y) 
        {
            return x + y;
        }

        static double Add(double x, double y) { 
            return x + y; 
        }
    }
}

 

posted @ 2025-06-14 08:51  市丸银  阅读(6)  评论(0)    收藏  举报