C#创建自己的扩展方法

C#可以创建自己的扩展方法Extension Method:

参考这篇《判断是否为空然后赋值http://www.cnblogs.com/insus/p/8004097.html

里,前面三个方法,均出现null这关键词,在判断时,你需要== null或者!=null。

其实你完全可以创建C#的扩展方法来消除这种的繁杂。

 

 public static class ExtensionMethod
    {
        public static bool IsNull(this object obj)
        {
            return obj == null;
        }

        public static bool IsNotNull(this object obj)
        {
            return obj != null;
        }
    }
Source Code


因此,前一篇中,可以产生方法五:


result = str.IsNull() ? "" : str;

            result = str.IsNotNull() ? str : "";
Source Code
原文地址:https://www.cnblogs.com/insus/p/8004351.html