关于C# sealed 和static和它的扩展方法

1.sealed其实跟Java的final关键字相似,被sealed和final修饰的类都不能被别的子类继承,C#里面为了内存安全性考虑,很多内部类都是sealed修饰,好比string类
2.static修饰的类不能实例化,也不能被继承,内部只能声明static修饰的成员变量和成员方法,最常用的类库项目比如sqlhelper这种,就最好声明为static类了,可以随时随地调用
3.注意扩展方法必须在静态类里面,扩展方法还是挺有b格的,在静态类里面可以声明扩展方法,声明的参数前面加this就表示是扩展方法 例如
public static bool checkstring(this string s)
看下调用方法

namespace 高级语法test
{
      static class Program
       {
        static void Main(string[] args)
        {
            string a = "123@qq.com";
            Console.WriteLine(a.checkstring());   //调用方法和传参的的方法  就好像string类型有checkstring这个方法一样
            Console.WriteLine(checkstring(a));
            Console.ReadKey();
        }

        public static bool checkstring(this string s)
        {
            return s.Contains("@");
        }
    }
}

其实就是调用的方法有点奇葩哈哈哈!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值