复习.Net高级
索引器:本质是封装了get,set方法,可以是只读或只写(在get或set前加上private),字符串是只读索引。 密闭类与静态类: 第一、密闭类是修饰为sealed的类,sealed不能有子类。String类无法被继承,因为String被声明了sealed。 第二、声明为static的类,不能实例化,只能定义static成员。通常用它定义扩展方法。 第三、C#3.0特性:扩展方法。声明静态类,增加一个静态方法,第一个参数是被扩展类型的标记为this,然后在其他类中可以直接调用。 1 static void Main(string[] args) 2 { 3 string str = "Hello World!"; 4 str.ShowMsg(str); 5 } 6 7 //扩展方法 8 9 static class MyString 10 { 11 public static void ShowMsg(this string a,string msg) 12 { 13 Console.WriteLine(msg); 14 } 15 } 给string增加一个ShowMsg() 深拷贝、浅拷贝: 深拷贝就是重新new一个对象,然后把之前的那个对象的属性值再重新赋值给这个用户。 1 static void Main(string[] args) 2 { 3 MyCopy copy1