标签:simple ini get stat 自定义 space ring strong 条件
Action 无返回值的系统泛型委托
namespace ConsoleApp1
{
public class UserInfo
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
private static List getInit()
{
return new List() {
new UserInfo(){ Id=1, Name="001小王",Age=18 },
new UserInfo (){ Id=2,Name="002大王",Age=20},
new UserInfo (){ Id=3,Name="003笨笨",Age=21},
new UserInfo (){ Id=4,Name="004通天塔",Age=24},
};
}
static void Main(string[] args)
{
List list = getInit();
list.ForEach(new Action(delegate (UserInfo ui) { Console.WriteLine(ui.Name); }));
Console.WriteLine("----------------------------------------------------------------------------");
list.ForEach(delegate (UserInfo ui) { Console.WriteLine(ui.Id+"|"+ui.Name); });
Console.WriteLine("----------------------------------------------------------------------------");
list.ForEach(u=> {
Console.WriteLine(u.Id + "|" + u.Name);
});
Console.ReadLine();
}
}
}
Predicate 返回bool值的系统泛型委托
namespace ConsoleApp1
{
public class UserInfo
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
static class Program
{
private static List getInit()
{
return new List() {
new UserInfo(){ Id=1, Name="001小王",Age=18 },
new UserInfo (){ Id=2,Name="002大王",Age=20},
new UserInfo (){ Id=3,Name="003笨笨",Age=21},
new UserInfo (){ Id=4,Name="004通天塔",Age=24},
};
}
static void Main(string[] args)
{
#region
List list = getInit();
list = list.FindAll(new Predicate(delegate (UserInfo u) { return u.Id > 1; }));
list.ForEach(u => {
Console.WriteLine(u.Id + "|" + u.Name);
});
Console.WriteLine("----------------------------------------------------------------------------");
list = list.FindAll(delegate (UserInfo u) { return u.Id > 2; });
list.ForEach(u => {
Console.WriteLine(u.Id + "|" + u.Name);
});
Console.WriteLine("----------------------------------------------------------------------------");
list=list.FindAll(u => u.Id > 3);
list.ForEach(u=> {
Console.WriteLine(u.Id + "|" + u.Name);
});
#endregion
Console.WriteLine("----------------------------自定义扩展方法---------------------------");
List listnew = list.MyFindAll(delegate (UserInfo u) { return u.Id > 3; });
listnew.ForEach(u => {
Console.WriteLine(u.Id + "|" + u.Name);
});
Console.ReadLine();
}
static List MyFindAll(this List list, Predicate predicate)
{
//新集合
List newlist = new List();
//遍历老集合
foreach (T item in list)
{
//如果item符合条件,则加入新集合
if (predicate(item))
{
newlist.Add(item);
}
}
return newlist;
}
}
}
Comparison 返回int类型
namespace ConsoleApp1
{
public class UserInfo
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
static class Program
{
private static List getInit()
{
return new List() {
new UserInfo(){ Id=1, Name="001小王",Age=18 },
new UserInfo (){ Id=2,Name="002大王",Age=20},
new UserInfo (){ Id=3,Name="003笨笨",Age=21},
new UserInfo (){ Id=4,Name="004通天塔",Age=24},
};
}
static void Main(string[] args)
{
List list = getInit();
list.Sort(delegate (UserInfo u1, UserInfo u2) {
return u2.Age - u1.Age;
});
list.ForEach(u =>
{
Console.WriteLine(u.Id + "|" + u.Name);
});
Console.ReadLine();
}
}
}
Func 自定义返回类型的系统泛型委托
namespace ConsoleApp1
{
public class UserInfo
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class UserSimple
{
public string Name { get; set; }
}
static class Program
{
private static List getInit()
{
return new List() {
new UserInfo(){ Id=1, Name="001小王",Age=18 },
new UserInfo (){ Id=2,Name="002大王",Age=20},
new UserInfo (){ Id=3,Name="003笨笨",Age=21},
new UserInfo (){ Id=4,Name="004通天塔",Age=24},
};
}
static void Main(string[] args)
{
List list = getInit();
IEnumerable uslist = list.Select(new Func(delegate (UserInfo u) { return new UserSimple() { Name = u.Name }; }));
uslist.ToList().ForEach(us =>
{
Console.WriteLine( "|" + us.Name);
});
Console.WriteLine("----------------------------------------------------------------------------");
IEnumerable newuslist = list.Select(delegate (UserInfo u) { return new UserSimple() { Name = u.Name }; });
uslist.ToList().ForEach(us =>
{
Console.WriteLine( "|" + us.Name);
});
Console.WriteLine("-------------------------------------自定义-------------------------------");
List listnew = list.MySelect(delegate(UserInfo u) { return new UserSimple() { Name = u.Name }; });
listnew.ForEach(us =>
{
Console.WriteLine( "|" + us.Name);
});
Console.ReadLine();
}
static List MySelect(this List list, Func func)
{
List listnew = new List
();
foreach (T1 item in list)
{
TR tr = func(item);
listnew.Add(tr);
}
return listnew;
}
}
}
c#系统泛型委托
标签:simple ini get stat 自定义 space ring strong 条件
原文地址:http://www.cnblogs.com/itmu89/p/7545440.html