zoukankan      html  css  js  c++  java
  • 隐式转换和显式转换及强制转换的区别

       string strType = "123";
      object objType= (object)strType;//可以不要,隐式转换, 要的为显式转换
      string strType2 = (string)objType; //必须要,显式转换
      int intType = (int)strType; //错误,不能通过编译
      int intType = (int)objType; //错误,能通过编译
      int intType = Convert.ToInt32(objType); //正确, 强制转换
      int intType = int.Parse(strType); //正确, 强制转换
      int intType = int.Parse(objType); //错误,不能通过编译
     --------------------------------------------------------------
      在 C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别?
      int 关键字表示一种整型,是32位的,它的 .NET Framework 类型为 System.Int32。
      (int)表示使用显式强制转换,是一种类型转换。当我们从 int 类型到 long、float、double 或decimal 类型,可以使用隐式转换,但是当我们从 long 类型到 int 类型转换就需要使用显式强制转换,否则会产生编译错误。
      Int32.Parse()表示将数字的字符串转换为32 位有符号整数,属于内容转换[1]。
      我们一种常见的方法:public static int Parse(string)。
      如果 string 为空,则抛出 ArgumentNullException 异常;
      如果 string 格式不正确,则抛出 FormatException 异常;
      如果 string 的值小于 MinValue 或大于 MaxValue 的数字,则抛出 OverflowException 异常。

      Convert.ToInt32() 则可以将多种类型(包括 object 引用类型)的值转换为 int 类型,因为它有许多重载版本[2]:
      public static int ToInt32(object);
      public static int ToInt32(bool);
      public static int ToInt32(byte);
      public static int ToInt32(char);
      public static int ToInt32(decimal);
      public static int ToInt32(double);
      public static int ToInt32(short);
      public static int ToInt32(long);
      public static int ToInt32(sbyte);
      public static int ToInt32(string);

      ......

      (int)和Int32.Parse(),Convert.ToInt32()三者的应用举几个例子:
      例子一:
      long longType = 100;
      int intType = longType; // 错误,需要使用显式强制转换
      int intType = (int)longType; //正确,使用了显式强制转换
      例子二:
      string stringType = "12345";
      int intType = (int)stringType; //错误,string 类型不能直接转换为 int 类型
      int intType = Int32.Parse(stringType); //正确
      例子三:
      long longType = 100;
      string stringType = "12345";
      object objectType = "54321";
      int intType = Convert.ToInt32(longType); //正确
      int intType = Convert.ToInt32(stringType); //正确
      int intType = Convert.ToInt32(objectType); //正确
      例子四[1]:
      double doubleType = Int32.MaxValue + 1.011;
      int intType = (int)doubleType; //虽然运行正确,但是得出错误结果
      int intType = Convert.ToInt32(doubleType) //抛出 OverflowException 异常
      (int)和Int32.Parse(),Convert.ToInt32()三者的区别:
      第一个在对long 类型或是浮点型到int 类型的显式强制转换中使用,但是如果被转换的数值大于 Int32.MaxValue 或小于 Int32.MinValue,那么则会得到一个错误的结果。
      第二个在符合数字格式的 string 到 int 类型转换过程中使用,并可以对错误的 string 数字格式的抛出相应的异常。
      第三个则可以将多种类型的值转换为 int 类型,也可以对错误的数值抛出相应的异常。
      无论进行什么类型的数值转换,数值的精度问题都是我们必须考虑的[1]。

      --------------------------------------------------------
      使用Convert.ToInt32()把一个char型转换成int时,是把这个char的ascci码给过去而不是数字
      如:
      char c = '1';
      int i;
      i = Convert.ToInt32(c); //char需注意的事项
      //这时i的值为49,是1的ascii码
      想得到1,可以使用string类型,
      如:
      string str= "1";
      int i;
      i = int.Parse(str);
      i = Convert.ToInt32(str);
      //这时i的值为1,而不是1的ascii码
      ----------------------------------------------------
      隐式转换各显式转换要求是同类型的,就是说两种数据类型必须兼容,隐式转换是向上转型(相当是子类转父类),而强制类型转换则是向下转型(相当是父类转子类),就好像Double型的可以包含int型一样。
      而强制转换可以是不是同一种类型,(如同class1与class2同级别的类一样),两都进行内容上的解析。Convert.ToInt32与int.Parse都是强制转换,int.Parse是转换String为int(这种情况很多,可能进行了些优化,也可能只是为了方便,处理逻辑一样), 而Convert.ToInt32是转换继承自Object的对象为int的(18种重载). 比如一个object对象,你想把它转换为int,用int.Parse就不可以,要用Convert.ToInt32
  • 相关阅读:
    ionic localstorage
    angular 中文鏈接
    把jqmobi 變成jQuery 的插件 從此使用jQuery
    jqmobi 的一些設置
    ionic ngcordova map 地圖
    ionic pull to refresh 下拉更新頁面
    json 對象的序列化
    鍵盤彈出,頁面佈局被推上去了.....
    Cordova V3.0.0中config.xml配置文件的iOS Configuration
    android ios 只能輸入數字 不能輸入小數點的 函數 cordova
  • 原文地址:https://www.cnblogs.com/tinya/p/4616722.html
Copyright © 2011-2022 走看看