C#_Delegate_泛型

//委托是一种数据类型  将方法作为参数进行传递

//我们定义委托 需要定义 委托的 返回值以及参数类型、个数

//利用delegate来定义

  public delegate void GetSthDelegate();

 

  void static Main()

  {

    GetSthDelegate gd=new GetSthDelegate(Function1);   

    //GetSthDelegate gd=Function1; 

    gd();  //或者

    gd.Invoke();

  }

  static void Function1()

  {

    print("This is f1.");

  }

//委托类型当作参数来使用

//----------------------------Action委托

  Action 系统预定义的委托类型    无参数   无返回值

  Action<int> a;  //定义了一个委托变量  有一个int参数  无返回值

  Action<string> a; //~~~~~~~~~~~~ ~~~~string~~ ~~~~

  //系统自动匹配方法  

  //Action 无返回值

  Action<int,int> a;    //定义了一个有两个int参数委托变量  无返回值

  //最大支持 16个参数

 

//---------------------多播委托--------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _09多播委托
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //将多个方法赋值给同一个委托变量
14             MyDelegate md = F1;
15             md += F2;
16             md += F3;
17 
18             //绑定到该委托变量上的所有方法都会被执行    顺序不一定
19             md.Invoke("cheng");
20 
21             Console.WriteLine("---------------------------");
22 
23             md -= F2;           //将绑定的方法去除
24             md.Invoke("Wang");
25         }
26 
27 
28        static void F1(string s)
29         {
30             Console.WriteLine(  "F1-->>{0}",s);
31         }
32        static void F2(string s) 
33        {
34            Console.WriteLine("F2+{0}",s);
35        }
36        static void F3(string s)
37        {
38            Console.WriteLine("F3++{0}",s);
39        }
40     }
41 
42 
43     public delegate void MyDelegate(string str);
44 
45 }
多播委托

//--------------------匿名方法----------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _08匿名方法
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             #region 委托就是一个继承自Delegate的类    必须赋值对应的对象
14 
15             ////委托类型只能赋值委托变量   Delegate1 d1=new Delegate(F1);    将方法作为委托的参数传递过去
16             //Delegate1 d1 = F1;
17 
18             ////F1();
19 
20             //d1.Invoke();
21 
22             //Delegate2 d2 = F2;
23             //d2.Invoke(12);
24 
25             //Delegate3 d3 = F3;
26             //int x = d3.Invoke(12, 23);
27 
28             //Console.WriteLine(x);
29             //Console.ReadKey(); 
30             #endregion
31 
32 
33             #region MyRegion
34             //Delegate3 d3 = F3;
35 
36             //int x =d3(10, 28);
37             //Console.WriteLine(x);
38             #endregion
39 
40 
41             #region 匿名方法和lambda表达式
42             //匿名方法
43             //Delegate1 d1 = delegate() {
44 
45             //    Console.WriteLine("我是匿名方法");
46 
47             //};
48             //d1();
49 
50             //Delegate4 d4 = delegate(int c)
51             //{
52             //    return c * 2;
53             //};
54             //d4(20);
55 
56             //lambda 表达式
57             //因为Delegate1 定义是无参数  无返回值  () 为空表示无参数       参数 =>  方法体
58             //Delegate1 d1 = () => { Console.WriteLine("我就是一个匿名函数"); };
59             //d1();
60 
61             //Delegate2 d2 = x => { Console.WriteLine("aaa{0}aaa",x); };
62             //d2(12);
63 
64             //Delegate3 d3 = (x, y) => { return x * y; };
65                 //Delegate3 d3 = (x, y) => x * y;
66             //int result = d3(12, 2);
67             //Console.WriteLine(result);
68 
69             Delegate4 d4=x=>10*x;
70             int res = d4(10);
71             Console.WriteLine(res);
72             #endregion
73         }
74 
75         static void F1()
76         {
77             Console.WriteLine("function1..");
78         }
79 
80         static void F2(int n)
81         {
82             Console.WriteLine("-->{0}",n);
83         }
84 
85         static int F3(int m,int n)
86         {
87             return m + n;
88         }
89 
90     }
91 
92     public delegate void Delegate1();
93     public delegate void Delegate2(int n);
94     public delegate int Delegate3(int m,int n);
95     public delegate int Delegate4(int n);
96 }
匿名方法、Lambda表达式

//---------------------自定义泛型------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _10自定义泛型
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //泛型是代码重用 也是算法重用       数据类型不同
14             //MyClass<int> mcInt = new MyClass<int>(new int[] { 1,2,3,4,5,6});
15             MyClass<string> mcStr = new MyClass<string>(new string[] { "Joe","Xu","Ling"});
16             mcStr.Show();
17         }
18     }
19 
20     //自定义泛型的类     T  代表  Type  类型 
21     class MyClass<T>
22     {
23 
24         public MyClass(T[] shuzu)
25         {
26             this._shuzu = shuzu;
27         }
28 
29         private T[] _shuzu;
30 
31         public void Show()
32         {
33             foreach (T item in _shuzu)
34             {
35                 Console.WriteLine(item.ToString());
36             }
37         }
38     }
39 }
自定义泛型

//--------------------泛型约束-------------------------

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace _11泛型约束
  8 {
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13             //在自定泛型之后   约束T可传入参数的数据类型
 14 
 15             //委托是针对一个方法多态   而接口可以针对n多个方法多态
 16 
 17             //pre练习
 18             //List<int> listInt = new List<int>();
 19             //listInt.Add(10);
 20             //listInt.Add(4);
 21             //listInt.Add(6);
 22             //listInt.Sort();
 23             //foreach (int item in listInt)
 24             //{
 25             //    Console.WriteLine(item);
 26             //}
 27 
 28 
 29             #region list排序
 30 
 31             //List<Person> listPerson = new List<Person>() { 
 32             //    new Person(){Name="wang",Age=32},
 33             //    new Person(){Name="ling",Age=23}
 34             //};
 35             ////要实现排序   需要实现IComparable
 36             //listPerson.Sort();
 37             //foreach (Person item in listPerson)
 38             //{
 39             //    Console.WriteLine("{0},{1}", item.Age, item.Name);
 40             //}
 41             //Console.WriteLine("--------------------------------------------");
 42             ////实现排序   也可以传递比较器
 43             //listPerson.Sort(new AscByAge());
 44             //foreach (Person item in listPerson)
 45             //{
 46             //    Console.WriteLine("{0},{1}", item.Age, item.Name);
 47             //} 
 48             #endregion
 49 
 50             //泛型约束         在泛型后面  添加   where   T   :     struct       //struct代表值类型   
 51 
 52             //不能限制具体的值类型   
 53             MyClass<double> testDou = new MyClass<double>();
 54 
 55             //在泛型后面添加   where T : class    //class代表T为引用类型        
 56             //在后面添加  where T  : IComparable     //代表T必须为实现某接口的类型
 57             TestClass<string> testStr = new TestClass<string>();
 58 
 59         }
 60     }
 61 
 62 
 63 
 64     //使用泛型约束   约束  T 只能是  值类型
 65     class MyClass<T> where T : struct
 66     {
 67  
 68     }
 69 
 70     //使用泛型约束   约束  T只能是 引用类型
 71     class TestClass<T> where T : class
 72     {
 73  
 74     }
 75 
 76     //约束  T 必须实现  IComparable
 77     class TestClass2<T> where T : IComparable
 78     {
 79  
 80     }
 81 
 82 
 83 
 84 
 85     //比较器
 86     class AscByAge : IComparer<Person>
 87     {
 88         public int Compare(Person x, Person y)
 89         {
 90             return y.Age - x.Age;
 91         }
 92     }
 93 
 94     //实现比较的泛型接口   传递的就是Person类型
 95     class Person:IComparable<Person>
 96     {
 97         private int _age;
 98         public int Age
 99         {
100             get { return _age; }
101             set { _age = value; }
102         }
103         private string _name;
104         public string Name
105         {
106             get { return _name; }
107             set { _name = value; }
108         }
109         //实现IComparable<Person>   实现泛型接口
110         public int CompareTo(Person other)
111         {
112            // throw new NotImplementedException();
113             return this.Age - other.Age;
114         }
115     }
116 }
泛型约束

 

转载于:https://www.cnblogs.com/siyi/p/4998795.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值