委托
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
//Class1 cdata = new Class1();
//int c = cdata.jiafa(1,2);
//Console.WriteLine(c);
////通过委托来调用
////接收对应条件的函数,然后可以用委托对象来替代函数执行函数操作。
//mydele m =cdata.jiafa;
//int s = m(3, 5);
//Console.WriteLine(s);
//mydele m1 = cdata.jianfa;
//Console.WriteLine(m1(5,3));
//多路委托:相当于一个集合再添加方法,删除方法,按照先后顺序执行方法,必须是同一种委托类型的变量
//mydele a, b, c;//a,b,c必须是同一个委托类型
//a = new Class1().jiafa;
//b = new Class1().jianfa;
//c = a + b;
//Console.WriteLine(c(1,2));
//匿名方法,泛型集合
//临时指定一个委托,临时写方法
//string s = "你好";
//Func<string, string> bianliang = delegate(string p)
// {
// p = p + s;
// return p;
// };
//Console.WriteLine(bianliang("张三"));
//Class1 c = new Class1();
//Class1 d = new Class1("很好");
//lambda表达式:指向的意思
Func<string, string,string> bianliang = (s1, s2) => s1 + "world" + s2;
Console.ReadLine();
}
}
}
类库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication21
{
//委托:detegete是一个函数的容器
public delegate int mydele(int a ,int b);
class Class1
{
public int jiafa(int a ,int b)
{
return a + b;
}
public int jianfa(int a, int b)
{
return a - b;
}
//类的构造函数
public Class1()
{
Console.WriteLine("这是类的构造函数");
}
}
}