在C#强制转换中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别?

int 关键字表示一种整型,是32位的,它的 .NET Framework 类型为 System.Int32。

(int)表示使用显式强制转换,是一种类型转换。当我们从int类型到long、float、double 或decimal 类型,可以使用隐式转换,但是当我们从long类型到int类型转换就需要使用显式强制转换,否则会产生编译错误。

Int32.Parse()表示将数字的字符串转换为32 位有符号整数,属于内容转换[1]。

(int32.parse和int.parse()没什么区别,int在C#默认32位)

我们一种常见的方法: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);    //正确

例子四:

double doubleType = Int32.MaxValue + 1.011;

int intType = (int)doubleType;                       //虽然运行正确,但是得出错误结果

int intType = Convert.ToInt32(doubleType)            //抛出 OverflowException 异常

C#强制转换中(int)和Int32.Parse(),Convert.ToInt32()三者的区别:

第一个在对long 类型或是浮点型到int 类型的显式强制转换中使用,但是如果被转换的数值大于Int32.MaxValue 或小于 Int32.MinValue,那么则会得到一个错误的结果。

第二个在符合数字格式的string到int 类型转换过程中使用,并可以对错误的string数字格式的抛出相应的异常。

第三个则可以将多种类型的值转换为int类型,也可以对错误的数值抛出相应的异常。

无论进行什么类型的数值转换,数值的精度问题都是我们必须考虑的。

更多:http://www.cnblogs.com/linyechengwei/archive/2008/11/10/1330819.html

asp.net将object或string转为int的更多相关文章

  1. string和int的相互转换方法

    string转为int string str = "100000"; stringstream ss; ss << str; int i; ss >> i; ...

  2. string与int的相互转换C++(转)

    string与int之间的相互转换C++(转) #include<iostream> #include<string> #include<sstream> usin ...

  3. [C#]List<int>转string[],string[]转为string

    // List<int>转string[] public string[] ListInt2StringArray(List<int> input) { return Arra ...

  4. Jackson将json string转为Object,org.json读取json数组

    从json文件读取json string或者自定义json string,将其转为object.下面采用的object为map,根据map读取json的某个数据,可以读取第一级的数据name,后来发现 ...

  5. C#中String转int问题

    String转int主要有四种方法 1. int.Parse()是一种类容转换:表示将数字内容的字符串转为int类型. 如果字符串为空,则抛出ArgumentNullException异常: 如果字符 ...

  6. String转int数字格式异常问题

     写在前面的话 差不多一年前就计划写博客,可因为种种原因一直没有写,反而我身边的一些同学在我建议他们写博客不久之后就写了,比如张博同学,基本每次总结一个知识点就写一篇,这样不但方便自己以后查看翻阅,也 ...

  7. golang学习笔记13 Golang 类型转换整理 go语言string、int、int64、float64、complex 互相转换

    golang学习笔记13 Golang 类型转换整理 go语言string.int.int64.float64.complex 互相转换 #string到intint,err:=strconv.Ato ...

  8. Object、String、数组的 toString() 方法和 equals() 方法及java.util.Arrays

    public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; in ...

  9. 15_常用API_第15天(Object、String、StringBuffer、用户登陆注册)_讲义

    今日内容介绍 1.Object 2.String 3.StringBuilder 01API概念 A:API(Application Programming Interface) 应用程序编程接口 B ...

随机推荐

  1. Leetcode OJ 刷题

    Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...

  2. Apache Mina开发手冊之四

    Apache Mina开发手冊之四 作者:chszs,转载需注明. 博客主页:http://blog.csdn.net/chszs 一.Mina开发的主要步骤 1.创建一个实现了IoService接口 ...

  3. Android四大组件之Activity详解

    一.Activity的概要说明 我看过Activity的源码,Activity类注释大概是这样解释的:几乎所有的Activity都是与用户交互用的,我想用了一个几乎的意思应该是排除一些纯展示界面吧,因 ...

  4. IIS网站发布容易出现的几个问题

    1. 更新版本或者重新安装.net Framework: 2. 更改配置文件节点: 3. 访问权限问题的更改:

  5. js 中日期 转换成时间戳 例如2013-08-30 转换为时间戳

    //时间格式2014-02-02 14:10:00改成时间戳 //此时构造出来的时间是:2013/03/08 00:00:00. //这样得到的是一个数值,表示的是从1970年1月1日0点0分0秒到d ...

  6. 百度下载google 浏览器安装失败

    installer integrity check has failed. Common causes include incomplete download and damaged media co ...

  7. CRM中的一个函数,保存一下,别系统被ぅ崩坏就麻烦了.

    CREATE OR REPLACE function UXQLCRM.GET_WEI_XIU(htfid in varchar2) ); CURSOR cr_bg_jl is select " ...

  8. Flex TextInput的restrict属性应用

    1,<mx:TextInput id="test_ti" width="160" maxChars="20" restrict=&qu ...

  9. 开源html5_kiwijs_helloworld

    本次须要的下载文件已经共享出来 网盘地址 由于我使用的是黑苹果系统, window我就无视了. 开发工具使用 网盘里的 dmg :Sublime Text 打开开发工具后在helloworld中找到 ...

  10. C#后台发送HTTP请求

    using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using Syst ...