.NETCore委托,C#委托的用法

1. 委托

C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针。委托(Delegate) 是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。委托(Delegate)特别用于实现事件和回调方法。所有的委托(Delegate)都派生自 System.Delegate 类。

1.1 委托的使用步骤

声明委托:委托声明决定了可由该委托引用的方法。委托可指向一个与其具有相同标签的方法。

public delegate int MyDelegate (string s);

实例化委托:委托对象必须使用 new 关键字来创建,且编写一个与委托签名相匹配的方法进行绑定。

调用委托:调用实例化的委托并提供与委托方法相符的参数

 public delegate int MyDelegate(string s);
 static void Main(string[] args)
 {
     //实例化委托
     MyDelegate md1 = new MyDelegate(MyDelFunction);
     //调用委托
     int i = md1("20240806!");
     Console.WriteLine(i);
 }
 /// <summary>
 /// 委托方法
 /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 public static int MyDelFunction(string s)
 {
     return s.Length;
 }

1.2 委托的多播形式

委托可以绑定多个方法同时执行,例如

 public delegate int MyDelegate(string s);
 static void Main(string[] args)
 {
     //实例化委托
     MyDelegate md1 = new MyDelegate(MyDelFunction);
     md1 += MyDelFunction1;
     md1 += MyDelFunction2;
     //调用委托o
     int i = md1("HellWorld!");
     //或者这样调用
     md1.Invoke("八嘎呀路");


 }
 /// <summary>
 /// 委托方法
 /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 public static int MyDelFunction(string s)
 {
     Console.WriteLine("委托方法1:" + s);
     return 0;
 }

 public static int MyDelFunction1(string s)
 {
     Console.WriteLine("委托方法2:"+s);
     return 0;
 }

 public static int MyDelFunction2(string s)
 {
     Console.WriteLine("委托方法3:" + s);
     return 0;
 }

执行结果:

2. 泛型委托

2.1 泛型

C#中的泛型:在C#中,泛型(Generics)允许定义类、接口、方法等,而不指定具体的类型,而是在使用时再指定。

泛型类,泛型方法的使用
 internal class Program
 {

    
     static void Main(string[] args)
     {
         Print<int> p1 = new ConsoleApp1.Print<int>();
         p1.PrintLine<string >("八嘎呀路");
         p1.PrintLine<double >(Math.PI );
     }
    
 }
 /// <summary>
 /// 打印
 /// </summary>
 /// <typeparam name="T"></typeparam>
 public class Print<T>
 {

     public void PrintLine<T>(T value)
     {
         Console.WriteLine(value);
     }

 }
泛型约束

可以对泛型类型参数进行约束,以限制可以使用的类型。常见的约束有:

  • where T : class:类型必须是引用类型。
  • where T : struct:类型必须是值类型。
  • where T : new():类型必须有一个无参数的构造函数。
  • where T : BaseClass:类型必须是 BaseClass 或其派生类。
  • where T : IInterface:类型必须实现 IInterface 接口。

泛型约束使用示例

public class GenericConstraintExample<T> where T : new()
{
    public T CreateInstance()
    {
        return new T();
    }
}

2.2 泛型委托 Action和Func委托

泛型委托的使用

在C#中,泛型委托(Generic Delegates)允许定义一个委托类型,而不指定具体的参数类型和返回类型,而是在使用时再指定。

//声明泛型委托 T为参数 ,TResult为返回结果
public delegate TResult  MyGenericDelegate<T, TResult>(T arg);

static void Main(string[] args)
{
    //使用泛型委托
    MyGenericDelegate<string, int> fanxingDel = FXFunction;
    Console.WriteLine(fanxingDel("20240806巴黎人民在犁地!"));

}

public static int  FXFunction(string text)
{
    return text.Length;
}

执行结果

Action 委托

Action 委托用于表示没有返回值的方法。它可以有零个或多个参数。

internal class Program
{
    static void Main(string[] args)
    {
        Action<string> printStringAction = ActionDelegateClass.PrintLine;
        printStringAction("塞纳河畔 阿里拉屎!");
        Action<int> printIntAction = ActionDelegateClass.PrintLine;
        printIntAction(20240806);
    }
}
/// <summary>
/// Action委托示例类 
/// </summary>
/// <typeparam name="T"></typeparam>
public class ActionDelegateClass
{
    public static void PrintLine(int printInt)
    {
        Console.WriteLine("打印Int:"+ printInt);
    }
    public static void PrintLine(string printString)
    {
        Console.WriteLine("打印String:" + printString);
    }
}

执行结果

Func 委托

Func 委托用于表示有返回值的方法。它可以有零个或多个参数,但最后一个类型是返回类型。

internal class Program
{
    static void Main(string[] args)
    {
       Func<string,string ,string> addFuncDelegate= FuncDelegateClass.Add;
        Console.WriteLine(addFuncDelegate("中国足球"," 他达个裘!"));

        Func<int, int, int> addIntFuncDelegate = FuncDelegateClass.IntAdd;
        Console.WriteLine(addIntFuncDelegate(250, 520));
    }
}
/// <summary>
/// Func委托示例类 
/// </summary>
/// <typeparam name="T"></typeparam>
public class FuncDelegateClass
{
    public static string Add(string  A,string B)
    {
        
        return (A + B).ToString();
    }
    public static int  IntAdd(int A,int B)
    {
        return (A + B);
    }
}

执行结果

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值