您的位置:首页 > 编程语言 > C#

C#委托,lambda表达式联系,

2017-04-05 23:25 281 查看
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
public class Product {
readonly string name;
public string Name { get { return name; } }
decimal price;
public Product(string name)
{
this.name = name;
}
public Product(int name,string str=null, int na=1)
{
this.price = name;
}
public override string ToString()
{
return string.Format("name:{0}", name);
}
}
class ProductNameComparer : IComparer<Product>
{
public int Compare(Product x, Product y)
{
return x.Name.CompareTo(y.Name);
}

}
class Program
{
delegate bool testDelegate(Product p);
static void Main(string[] args)
{
List<Product> productList = new List<Product> {
new Product(name: "dddd"),
new Product(name: "aaaa"),
new Product(name: "eeee"),
new Product(name: "nnnn"),
new Product(name: "uuuu"),
};
//productList.Sort(new ProductNameComparer());
//productList.Sort(delegate (Product x, Product y) { return x.Name.CompareTo(y.Name); });
//productList.Sort((x,y) => x.Name.CompareTo(y.Name));
Console.WriteLine("--------------------排序------------------");
foreach (var product in productList.OrderBy(p => p.Name))
{
Console.WriteLine(product.ToString());
}
Console.WriteLine("------------------------------------------");
((productList.OrderBy(p => p.Name)).ToList<Product>()).ForEach(Console.WriteLine);
Console.WriteLine("--------------------输出------------------");
productList.ForEach(Console.WriteLine);
Console.WriteLine("--------------------查找------------------");

Action<Product> print = Console.WriteLine;
Predicate<Product> test = delegate (Product p) { return p.Name.Contains("ddd"); };//Predicate返回bool型

Predicate<Product> test2 = (p) => p.Name.Contains("ddd");
Predicate<Product> test3 = IsContanins;
Func<Product, bool> testFun = (p) => p.Name.Contains("ddd");//Func,有返回类型
Func<Product, bool> testFun2 = delegate (Product p) { return p.Name.Contains("ddd"); };//delegate可以自定义,这个写法中省略了delegate的返回类型
testDelegate testDelegate1=(p)=>p.Name.Contains("ddd");//delegate只能在函数外定义
Func<Product, bool> testFun3 = (p)=> testDelegate1(p);
Predicate<Product> test4 = (p) => testFun(p);//lambda表达式中用Func

List<Product> matches = productList.FindAll(test);
List<Product> matches2 = productList.FindAll(test4);
List<Product> matches3 = productList.Where((p) => p.Name.Contains("ddd")).ToList();

foreach (var item in productList.Where((p) => p.Name.Contains("ddd")))//Linq
{
print(item);
}
Console.WriteLine("------------------------------------------");
matches.ForEach(print);
Console.WriteLine("------------------------------------------");
matches2.ForEach(print);
Console.WriteLine("------------------------------------------");
productList.FindAll((p) => p.Name.Contains("ddd")).ForEach(Console.WriteLine);

}
static bool IsContanins(Product test)
{
return test.Name.Contains("ddd");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# lambda 委托 delegate Func