博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2.C#中通过委托Func消除重复代码
阅读量:7103 次
发布时间:2019-06-28

本文共 3270 字,大约阅读时间需要 10 分钟。

阅读目录

   一:重复的代码

 二:C#中通过委托Func消除重复代码

   一:重复代码
   

1 public class Persion 2     { 3         public string Name { get; set; } 4         public int Age { get; set; } 5  6         public Persion GetPersionInfo() 7         { 8             try 9             {10                 Persion persion = new Persion();11                 persion.Name = "David";12                 persion.Age = 30;13 14                 return persion;15             }16             catch (Exception ex)17             {18                 return null;19             }           20         }21     }

 

1  public class Employee 2     { 3         public string Post { get; set; } 4         public int Salary { get; set; } 5  6         public Employee GetEmployeeInfo() 7         { 8             try 9             {10                 Employee employee = new Employee();11                 employee.Post = "开发";12                 employee.Salary = 5000;13 14                 return employee;15             }16             catch(Exception ex)17             {18                 return null;19             }20         }21     }

  二:C#中通过委托Func消除重复代码

     如下所示,try catch 语句只有一次了,这样就消除了GetPersionInfo方法和GetEmployeeInfo的try catch语句

1 public class Utillity 2     { 3         public static T TryExecute
(Func
func, string methodName) 4 { 5 try 6 { 7 return func(); 8 } 9 catch (Exception ex)10 {11 Console.WriteLine("MethodName:" + methodName + " Error:" + ex.Message);12 return default(T);13 }14 }15 }

 

1 public class Persion 2     { 3         public string Name { get; set; } 4         public int Age { get; set; } 5  6         public Persion GetPersionInfo() 7         { 8             return Utillity.TryExecute
(() => 9 {10 Persion persion = new Persion();11 persion.Name = "David";12 persion.Age = 30;13 14 return persion;15 }16 , "GetPersionInfo");17 }18 }
1 public class Employee 2     { 3         public string Post { get; set; } 4         public int Salary { get; set; } 5  6         public Employee GetEmployeeInfo() 7         { 8             return Utillity.TryExecute(() => 9             {10                 Employee employee = new Employee();11                 employee.Post = "开发";12                 employee.Salary = 5000;13 14                 return employee;15             }16             , "GetEmployeeInfo");17 18         }19     }
1  class Program 2     { 3         static void Main(string[] args) 4         { 5             Persion persion1 = new Persion(); 6             Persion persion2 = persion1.GetPersionInfo(); 7  8             Console.WriteLine("This persion name is {0}", persion2.Name); 9             Console.WriteLine("This persion age is {0}", persion2.Age);10 11             Employee employee1 = new Employee();12             Employee employee2 = employee1.GetEmployeeInfo();13 14             Console.WriteLine("This employee post is {0}", employee2.Post);15             Console.WriteLine("This employee salary is {0}", employee2.Salary);16 17             Console.ReadLine();18         }19     }

 

    

转载地址:http://smkhl.baihongyu.com/

你可能感兴趣的文章
php单例模式的使用场景,使用方法
查看>>
fetch请求get方式以及post提交参数为formdata类型的数据
查看>>
[学习笔记]凸优化/WQS二分/带权二分
查看>>
CentOS 下 LVS集群( 可能更新 )
查看>>
差分信号(Differential Signal)
查看>>
Aix项目_shell_rsh_01
查看>>
HDU 5726 GCD 求给定序列中与查询段相等的GCD个数
查看>>
JavaScript基础-2
查看>>
python实训第四天
查看>>
5-4-3原则
查看>>
html图像入门
查看>>
C# Mongo Client 2.4.2创建索引
查看>>
我的第四个网页制作:列表标签
查看>>
【python进阶】详解元类及其应用2
查看>>
简单实用的菜单栏
查看>>
AMap行政区查询服务
查看>>
SpringBoot2.0源码分析(一):SpringBoot简单分析
查看>>
艾伟:这下没理由嫌Eval的性能差了吧?
查看>>
Java,net上的几篇文章
查看>>
Chrome的Awesome Screenshot的插件离线下载
查看>>