首页 > 代码库 > C# Func<T,TResult>
C# Func<T,TResult>
using System; namespace FuncDemo { internal class Program { private static void Main() { //类似委托功能 Func<InputArgs, Result> func = TsetFunction; Console.WriteLine("第一种方式:"); Console.WriteLine(func(new InputArgs("zhangqs008", "123456"))); Console.WriteLine("============================================="); Console.WriteLine("第二种方式:"); CallMethod(func, new InputArgs("zhangqs008", "1234567")); //或者 CallMethod(TsetFunction, new InputArgs("zhangqs008", "1234567")); Console.Read(); } public static Result TsetFunction(InputArgs input) { Result result = new Result(); result.Flag = String.Compare("zhangqs008", input.UserName, StringComparison.OrdinalIgnoreCase) == 0 & String.Compare("123456", input.Password, StringComparison.OrdinalIgnoreCase) == 0; result.Msg = "当前调用者:" + input.UserName; return result; } public static void CallMethod<T>(Func<T, Result> func, T item) { Result result = func(item); Console.WriteLine(result.ToString()); } /// <summary> /// 方法参数 /// </summary> public class InputArgs { public InputArgs(string userName, string password) { UserName = userName; Password = password; } public string UserName { get; set; } public string Password { get; set; } } /// <summary> /// 方法结果 /// </summary> public class Result { public string Msg { get; set; } public bool Flag { get; set; } public override string ToString() { return string.Format("Flag:{0},Msg:{1}", Flag, Msg); } } } }
http://blog.csdn.net/a497785609/article/details/8282785
C# Func<T,TResult>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。