using :
1:引入命名空间
1 using System;
2:使用了一个对像后自动调用其IDespose
1 using (SqlCommand cmd = new SqlCommand()) 2 { 3 //执行代码 4 }
new:
运算符:用于创建对象和调用构造函数(实例化一个对象)
1 StringBuilder str = new StringBuilder();
修饰符:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员
1 public class Car 2 { 3 public string Name { get; set; } 4 public void PrintName() 5 { 6 Console.WriteLine($"this Car name is {Name}."); 7 } 8 } 9 public class FlyCar : Car 10 { 11 public new void PrintName() //隐藏基类中的PrintName() 12 { 13 Console.WriteLine($"this FlyCar name is {Name}."); 14 } 15 }
1 static void Main(string[] args) 2 { 3 FlyCar fyCar = new FlyCar(); 4 fyCar.Name = "CoCo"; 5 fyCar.PrintName(); //执行结果:this FlyCar name is CoCo. 6 }
约束:T(例如Fish)必须有public构造函数,不然会编译报错
1 public class Car<T> where T:new() 2 { 3 public T GetCar() 4 { 5 return new T(); 6 } 7 }
1 public class Fish 2 { 3 public Fish() { } 4 }
1 static void Main(string[] args) 2 { 3 Car<Fish> fish = new Car<Fish>(); 4 }