C#3.0里的扩展方法的THIS
static class MyClass
{
static void Print(this stirng s)
{
Console.WriteLine(s);
}
static void Main(string[] args)
{
stirng str = "dsdgs";
str.Print();
}
}
//注意上面的类是静太的
class A
{
public A()
{ }
public A(string name):this()// 2调用有参的构造函数先去调用无参的
{}
}
引用当前类的成员变量:this.name //3
将对象作为参数传递到其他方法 : add(this); //4
声明索引器: //5
public int this [int param]
{
get { return array[param]; }
set { array[param] = value; }
}
Using有3种用法:
Using System;//引用命名空间 using RES = System.Text.RegularExpressions.Regex; //简化命名空间 using(DataSet ds = new DataSet())//using语名块,执行代码块后自动销毁该对象 {}
new关键字有3种用法:
泛型约束,表T类型一定是引用类型且要带一个无参的构造函数 class A<T> where T : new() { } 实例化对象 Stuent stu = new Student();
隐藏从基类继承的同名成员和同名方法:
public class DerivedC : BaseC
{ public class BaseC
{ public int x;
public void Invoke() {}
}
new public int x;
new public void Invoke() {}
}