码迷,mamicode.com
首页 > Windows程序 > 详细

C#扩展方法this

时间:2020-01-11 15:10:20      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:app   一个   ogr   star   arp   rds   assert   www   rem   

转:https://www.cnblogs.com/ScottLin/p/10175513.html

先在StringLibrary类中定义一个静态方法,如下:

技术图片
public static class StringLibrary
    {
      //第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀,当前作用于String类型,也可作用于自定义类型 public static bool StartWithUpper(this String str) { if (String.IsNullOrEmpty(str)) return false; char ch = str[0]; return Char.IsUpper(ch); } }
技术图片

接着调用:

技术图片
[TestMethod]
        public void TestDoesNotStartWithUpper()
        {
            string[] words = { "alphabet", "Error", "zebra", "Abc", "αυτοκινητοβιομηχαν?α", "государство",
                   "1234", ".", ";", " " };

            foreach (var word in words)
            {
                bool result = word.StartWithUpper();
                Assert.IsFalse(result, $"Expected for ‘{word}‘: false; Actual:{result}");
            }
        }
技术图片

 具体可参考:C#扩展方法

 

C#扩展方法this

标签:app   一个   ogr   star   arp   rds   assert   www   rem   

原文地址:https://www.cnblogs.com/wangle1001986/p/12179790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!