.net中的委托方法
Func 接受输入参数(可多个残数),并且返回指定的输出TResult
Action接受参数(只有一个),返回void
Predicate 有一个输入参数,如果输入参数符合指定的条件,返回true,否则fasle.
具体代码实现如下:
一,public delegate TResult Func<in T, out TResult>( T arg )
封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。
实现一,显式声明自定义委托实现字符转换
using System;
delegate string ConvertMethod(string inString);
public class DelegateExample
{
public static void Main()
{
// 转成大写
ConvertMethod convertMeth = UppercaseString;
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMeth(name));
}
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}
使用匿名方法:
using System;
public class DelegateExample
{
public static void Main()
{
// 转成大写
Func<string,string> convertMeth = delegate(string inputstr)
{
return inputstr.ToUpper();
}
string name = "Dakota";
Console.WriteLine(convertMeth(name));
}
}
使用lambda表达式
using System;
public class lambdaExample
{
public static void Main()
{
// 转成大写
Func<string,string> convert = inputstr=>inputstr.ToUpper();
string name = "Dakota";
Console.WriteLine(convert(name));
}
}
示例again
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
static class Func
{
static void Main(string[] args)
{
Func<string, string> selector = str => str.ToUpper();
// 建立一个字符串数组
string[] words = { "orange", "apple", "Article", "elephant" };
IEnumerable<String> aWords = words.Select(selector);
//输出
foreach (String word in aWords)
Console.WriteLine(word);
}
}
二,public delegate void Action<in T>( T obj )
该方法只有一个参数并且不返回值,
T为此委托封装的方法的参数类型。此类型参数是逆变。
using System;
using System.Windows.Forms;
public class TestAnonMethod
{
public static void Main()
{
Action<string> messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = delegate(string s) { MessageBox.Show(s); };
else
messageTarget = delegate(string s) { Console.WriteLine(s); };
//lambda表达式
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = s => MessageBox.Show(s);
else
messageTarget = s => Console.WriteLine(s);
messageTarget("Hello, World!");
}
}
//打印list<string>
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<String> names = new List<String>();
names.Add("Bruce");
names.Add("Alfred");
names.Add("Tim");
names.Add("Richard");
// 使用print答应list的值
names.ForEach(Print);
// 同上,使用匿名方法
names.ForEach(delegate(String name)
{
Console.WriteLine(name);
});
//同上, lambda表达式
names.ForEach(name=>console.WriteLine(name));
}
private static void Print(string s)
{
Console.WriteLine(s);
}
}
Predicate 声明:public delegate bool Predicate<in T>(T obj)
表示定义一组条件并确定指定对象是否符合这些条件的方法。
public class HockeyTeam
{
private string _name;
private int _founded;
public HockeyTeam(string name, int year)
{
_name = name;
_founded = year;
}
public string Name
{
get { return _name; }
}
public int Founded
{
get { return _founded; }
}
}
public class Example
{
public static void Main()
{
List<HockeyTeam> teams = new List<HockeyTeam>{
new HockeyTeam("Detroit Red Wings", 1926),
new HockeyTeam("Chicago Blackhawks", 1926),
new HockeyTeam("San Jose Sharks", 1991),
new HockeyTeam("Montreal Canadiens", 1909),
new HockeyTeam("St. Louis Blues", 1967) };
//1 >>1,2,3三种方式都可实现,选择其中一种就行。
Random rnd = new Random();
int[] years = { 1920, 1930, 1980, 2000 };
int foundedBeforeYear = years[rnd.Next(0, years.Length)];
Console.WriteLine("Teams founded before {0}:", foundedBeforeYear);
foreach (var team in teams.FindAll(x => x.Founded <= foundedBeforeYear))
Console.WriteLine("{0}: {1}", team.Name, team.Founded);
//2 //如果找到与指定谓词定义的条件匹配的第一个元素Array.Find(HockeyTeam,Predicate<HockeyTema>)
Predicate<HockeyTeam> find = FindTeam;
HockeyTeam ht = Array.Find(teams.ToArray(), find);
Console.WriteLine("{0}: {1}", ht.Name, ht.Founded);
//3
//Random rnd = new Random();
//int[] years = { 1920, 1930, 1980, 2000 };
//int foundedBeforeYear = years[rnd.Next(0, years.Length)];
//HockeyTeam ht4 = teams.Find(t => t.Founded < foundedBeforeYear);
//Console.WriteLine("{0}: {1}", ht4.Name, ht4.Founded);
}
private static bool FindTeam(HockeyTeam ht)
{
Random rnd = new Random();
int[] years = { 1920, 1930, 1980, 2000 };
int foundedBeforeYear = years[rnd.Next(0, years.Length)];
return ht.Founded <= foundedBeforeYear;
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。