流浪のwolf

卷帝

导航

.Net 的扩展方法

// 扩展方法 
// ps:js中的扩展方法 比如 给数组定义一个自定义的全局的方法 使用 prototype (原型链)
// .Net 给 string 添加一些扩展方法  String 是一个密封的类(不让继承的类,有 Sealed 关键字)
// string Array null 等在 System 命名空间下面;
// 扩展方法一定是在静态类里面
string text = "";
if(text.IsNotNullOrEmpty()){
}
// 使用扩展方法可以方便使用链式编程

// 定义一个静态类和静态方法 (对string扩展的类)以Extensions结尾
public static class StringExtensions{
    // 参数 -- 要扩展的对象
    public static bool IsNotNullOrEmpty(this String value){
        return !string.IsNullOrEmpty(value);
    }
}

 

posted on 2024-03-09 15:48  流浪のwolf  阅读(15)  评论(0编辑  收藏  举报