default(T) 和 typeof 和 GetType()

一、default(T) 在泛型编成中如果不限制T类型参数是值类型或引用类型的话 你程序内部可能会出现错误,
因为值类型不允许NULL.所以default用来获取一个类型的默认值,对于值类型得到new T()  基本得到的都是0;
对于引用类型会得到Null
或者你不使用Default关键词,自己通过反射得到T是指类型还是引用类型 然后设置默认值

 

二、

1、比如说吧,你有两个类MyClass1和MyClass2
MyClass1 class1 = new MyClass1();

那么 class1 == typeof(Myclass1) 就是true
同理 class1 == typeof(MyClass2) 就是flase

 

2、也就是获取一些类型,委托用得比较多,有时候判断语句也可以用到
比如panel1事件,但是我定义了很多委托
panel1.click +=new eventhandler(eventhandler pa_click);
panel2.click +=new eventhandler(eventhandler pa_click);
....
//注意这里的 panel1 panel2都是事件pa

void pa_click(.....)//单击事件
{
type pan=new typeof(panel);
  switch(pan)
{
case panel1:
  处理的事件;
  break;
case panel2:
    处理的事件;
  break;
....
}//希望这个例句对你有用,加油学啊!

 

 

Type t = typeof(SampleClass);相当于
      // Alternatively, you could use
      // SampleClass obj = new SampleClass();
      // Type t = obj.GetType();

 

 

 

3、GetType 返回当前实例的运行时类型。

posted on 2014-09-23 10:01  topguntopgun  阅读(187)  评论(0编辑  收藏  举报

导航