首页 > 代码库 > 扩展方法
扩展方法
利用扩展方法,实现参数验证。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 扩展方法 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string name = Console.ReadLine(); 14 name.Required(); 15 Console.WriteLine("你好," + name); 16 Console.ReadKey(); 17 } 18 } 19 20 public static class Validate 21 { 22 public static void Required(this string data) 23 { 24 if (string.IsNullOrEmpty(data)) 25 { 26 //Console.WriteLine("姓名不能为空"); 27 throw new ArgumentException("参数不能为空。"); 28 } 29 } 30 } 31 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。