【www.gdgbn.com--安卓教程】

int32.parse()、convert.toint32()、int32.tryparse()

    int 关键字表示一种整型,是32位的,它的 .net framework 类型为 system.int32。
int32.tryparse()不抛异常,会返回true或false来说明解析是否成功,如果解析错误,调用方将会得到0值。
    (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、(int)和int.parse三者的区别:

前者适合将object类类型转换成int类型,如convert.toint32(session["shuzi"]);

(int)适合简单数据类型之间的转换;

int.parse适合将string类类型转换成int类型,如int.parse(session["shuzi"].tostring())。
 

(1)这两个方法的最大不同是它们对null值的处理方法:
    convert.toint32(null)会返回0而不会产生任何异常,但int.parse(null)则会产生异常。

没搞清楚convert.toint32和int.parse()的细细微区别时千万别乱用,否则可能会产生无法预料的结果,举例来说:假如从url中取一个参数page的值,我们知道这个值是一个int,所以即可以

用convert.toint32(request.querystring["page"]),也可以用,int.parse(request.querystring["page"]),但是如果page这个参数在url中不存在,那么前者将返回0,0可能是一个有效的值

,所以你不知道url中原来根本就没有这个参数而继续进行下一下的处理,这就可能产生意想不到的效果,而用后一种办法的话没有page这个参数会抛出异常,我们可以捕获异常然后再做相应

的处理,比如提示用户缺少参数,而不是把参数值当做0来处理。

(2)还有一点区别就是
  a. convert.toint32(double value)
如果 value 为两个整数中间的数字,则返回二者中的偶数;即 3.5转换为4,4.5 转换为 4,而 5.5 转换为 6。  不过4.6可以转换为5,4.4转换为4
  b. int.parse("4.5")
直接报错:"输入字符串的格式不正确".

  c. int(4.6) = 4
int转化其他数值类型为int时没有四舍五入,强制转换

int.parse是转换string为int
convert.toint32是转换继承自object的对象为int的.
你得到一个object对象,你想把它转换为int,用int.parse就不可以,要用convert.toint32.

个人总结:
(1)convert.toint32的参数比较多,int.parse只能转换string类型的.
(2)parse就是把string转换成int,char,double....等,也就是*.parse(string) 括号中的一定要是string.
(3)convert可以提供多种类型的转换,也就是convert.*()括号中可以为很多种类型(包括string).

本文来源:http://www.gdgbn.com/shoujikaifa/29546/