public void Test() {
//泛型静态成员共享,aa.Count=bb.Count
var aa = new MyList<int>();
var bb = new MyList<int>();
var cc = new MyList<string>();
//返回int
Func<int, int, int> add = Add;
//无返回
Action<string> print = Print;
print(add(1, 2).ToString());
//Predicate,返回bool型的泛型委托
Point[] points = { new Point(100, 200), new Point(150, 250), new Point(250, 375), new Point(275, 395), new Point(295, 450) };
Point first = Array.Find(points, ProductGT10);
//委托1
var add2 = new Func<int, int, int>(delegate (int x, int y) { return x + y; });
var print2 = new Action<string>(delegate (string msg) { Console.WriteLine(msg); });
print2(add2(22, 33).ToString());
//委托2
Func<int, int, int> add3 = delegate (int x, int y) { return x + y; };
Action<string> print3 = delegate (string msg) { Console.WriteLine(msg); };
print3(add3(33, 44).ToString());
//Lambda表达式
Func<int, int, int> add4 = (x, y) => { return x + y; };
Action<string> print4 = (msg) => { Console.WriteLine(msg); };
Action<string> print5 = msg => { Console.WriteLine(msg); };//单个参数可以省略"()"
print4(add4(44, 55).ToString());
print5(add4(55, 66).ToString());
var students = new List<Student>();
var name = "";
//第一种
var newaa = students.Find(new Predicate<Student>(delegate (Student student) {
if (student.Name == name) return true;
return false;
}));
//第二种
newaa = students.Find(new Predicate<Student>((student) => {
if (student.Name == name) return true;
return false;
}));
//第三种
newaa = students.Find((student) => {
if (student.Name == name) return true;
return false;
});
//第四种
newaa = students.Find((student) => student.Name == name);
//表达式树
var para1 = Expression.Parameter(typeof(int), "x");
var para2 = Expression.Parameter(typeof(int), "y");
var paras = new ParameterExpression[] { para1, para2 };
var body = Expression.Add(para1, para2);
var expression = Expression.Lambda(body, paras);
var func = expression.Compile() as Func<int, int, int>;
var res = func(1, 2);
}
public static int Add(int i, int j) {
return i + j;
}
public static void Print(string msg) {
Console.WriteLine(msg);
}
private static bool ProductGT10(Point p) {
if (p.X * p.Y > 100000) {
return true;
} else {
return false;
}
}
public class MyList<T> {
T t = default(T);
public static int Count;
public MyList() {
Count++;
}
}