zoukankan      html  css  js  c++  java
  • 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 返回当前实例的运行时类型。

  • 相关阅读:
    jstl 部分标签
    Maven pom.xml 元素配置说明(一)
    spring 参数绑定
    mysql 索引
    ArrayList和HashSet的Contains()方法(转)
    每日记载内容总结44
    剑指offer42:不用加减乘除做加法
    动态规划常见题型
    华为机试-统计每个月兔子的总数
    华为机试-字符串合并处理
  • 原文地址:https://www.cnblogs.com/topguntopgun/p/3987696.html
Copyright © 2011-2022 走看看