Home  >  Article  >  Backend Development  >  Sample code sharing for generic delegation in c# (picture)

Sample code sharing for generic delegation in c# (picture)

黄舟
黄舟Original
2017-03-29 11:23:291260browse

This article mainly introduces the generic delegate in c#. It has a very good reference value. Let’s take a look at it with the editor.

Today we will learn about generic delegation in c#.

1. A general delegate, delegate, can pass in parameters (<=32), and the declared method is public delegate void SomethingDelegate(int a);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delegateSummary {
 public delegate void GetIntDelegate(int a); //声明一个委托
 public class getIntClass { 
 public static void SetDelegateString(int a,GetIntDelegate getIntDelegate) {//使用委托
  getIntDelegate(a);
 }
 public void getInt1(int a) { //方法1
  Console.WriteLine("getInt1方法调用,参数为:" + a);
 }
 public void getInt2(int a) { //方法2
  Console.WriteLine("getInt2方法调用,参数为:" + a);
 }
 }
 class Program {
 static void Main(string[] args) {
  getIntClass gc=new getIntClass();
  getIntClass.SetDelegateString(5, gc.getInt1);  //方法1,2作为委托的参数
  getIntClass.SetDelegateString(10, gc.getInt2); 
  Console.WriteLine("=====================");
  GetIntDelegate getIntDelegate;
  getIntDelegate = gc.getInt1;     //将方法1,2绑定到委托
  getIntDelegate += gc.getInt2;
  getIntClass.SetDelegateString(100, getIntDelegate); 
  Console.Read();
 } 
 }
}

Output the results. Note the difference between the two methods. The first method uses the method as a parameter of the delegate, and the second method binds the method to the delegate.

2. Generic delegate Action can pass in up to 16 parameters and has no return value.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delegateSummary {
 class Program {
 static void Main(string[] args) {
  TestAction<string>(getString, "WhiteTaken"); //传入方法
  TestAction<int>(getInt, 666);
  TestAction<int, string>(getStringAndInt, 666, "WhiteTaken");
  Console.Read();  
 }
 public static void TestAction<T>(Action<T> action,T p1) {        //Action传入一个参数测试
  action(p1);
 }
 public static void TestAction<T, P>(Action<T, P> action, T p1, P p2) { //Action传入两个参数测试
  action(p1,p2);
 }
 public static void getString(string a) {    //实现int类型打印
  Console.WriteLine("测试Action,传入string,并且传入的参数为:" +a);
 }
 public static void getInt(int a) {     //实现String类型打印
  Console.WriteLine("测试Action,传入int,并且传入的参数为:" + a);
 }
 public static void getStringAndInt(int a, string name) {    //实现int+string类型打印
  Console.WriteLine("测试Action,传入两参数,并且传入的参数为:" + a+":"+name);
 }
 }
}

Test results:

3. The Func of the generic delegate can pass in up to 16 parameters and have a return value. (The writing method is similar to Action, but with an additional return value)

4. The predicate of the generic delegate (not very commonly used), the return value is bool, used in ArraySearch elements in # and list. (I haven’t used it yet, I’ll update it after I use it)

The above is the detailed content of Sample code sharing for generic delegation in c# (picture). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn