请问关于c的具体应用场景有哪些?

2026-05-20 09:581阅读0评论SEO问题
  • 内容介绍
  • 相关推荐

本文共计602个文字,预计阅读时间需要3分钟。

请问关于c的具体应用场景有哪些?

今天学习了一下c语言基础。

今天学习一下c#中的泛型委托。

1.一般的委托,delegate,可以又传入参数(<=32),声明的方法为 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(); } } }

输出结果,注意两种方式的不同,第一种将方法作为委托的参数,第二种是将方法绑定到委托。

2.泛型委托之Action,最多传入16个参数,无返回值。

请问关于c的具体应用场景有哪些?

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); } } }

测试结果:

3.泛型委托之Func,最多传入16个参数,有返回值。(写法与Action类似,只是多了返回值)

4.泛型委托之predicate(不是很常用),返回值为bool,用在Array和list中搜索元素。(没有用到过,等用到了再更新)

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持易盾网络!

本文共计602个文字,预计阅读时间需要3分钟。

请问关于c的具体应用场景有哪些?

今天学习了一下c语言基础。

今天学习一下c#中的泛型委托。

1.一般的委托,delegate,可以又传入参数(<=32),声明的方法为 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(); } } }

输出结果,注意两种方式的不同,第一种将方法作为委托的参数,第二种是将方法绑定到委托。

2.泛型委托之Action,最多传入16个参数,无返回值。

请问关于c的具体应用场景有哪些?

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); } } }

测试结果:

3.泛型委托之Func,最多传入16个参数,有返回值。(写法与Action类似,只是多了返回值)

4.泛型委托之predicate(不是很常用),返回值为bool,用在Array和list中搜索元素。(没有用到过,等用到了再更新)

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持易盾网络!