int、int.Parse()、Convert.ToInt32()区别 //作者:马超

转载自:http://wenku.baidu.com/link?url=ygWc786h00fpaxVOEjgHEBaTefuN4DkX5wNyAjXCt09HE4WljQlFs4UmFQ3e58_48ziUhiUDm7IvKP_9cib2brVMPqVbEZe0vgtvcNBPl2e

int、int.Parse()、Convert.ToInt32()区别...

       直到自己亲自测试了才对他们有所了解,以前就知道用最后面那个,因为怕转化出错,所以就用它比较安全。

1.int :(int)变量,C#默认整型为int32;(不支持bool转化)

2.int.Parse(string sParameter) 4个构造函数,参数类型只支持string类型;

3.Convert.ToInt32()支持的类型是object

事例:

using System;
using System.Collections.Generic;
using System.Text;

namespace IntegerSample
{
    public class Program
    {
        private enum eNumber { Man = 1,Woman = 2 };

       private static char cNumber = 'e';
        private static byte btNumber = 12;
        private static long lNumber = 12345;
        private static double dNumber =12.34d;
        private static decimal dlNumber =4.5m;
        private static bool bNumber = true;

       private static string str = null;
        private static string str1 ="";
        private static string str2 ="123";
        private static string str3 ="Hello";

       public static void TransformByInt()
        {
           Console.WriteLine("{0} TransformByInt Into {1}", eNumber.Man,(int)eNumber.Man);
           Console.WriteLine("{0} TransformByInt Into {1}", cNumber,(int)cNumber);
           Console.WriteLine("{0} TransformByInt Into {1}", btNumber,(int)btNumber);
           Console.WriteLine("{0} TransformByInt Into {1}", lNumber,(int)lNumber);
           Console.WriteLine("{0} TransformByInt Into {1}", dNumber,(int)dNumber);
           Console.WriteLine("{0} TransformByInt Into {1}", dlNumber,(int)dlNumber);
           //Console.WriteLine("{0} TransformByInt Into {1}", bNumber,(int)bNumber);
            //Console.WriteLine("{0}TransformByInt Into {1}", str1, (int)str);
        }

       // Result: 1
        // Result
:101
        // Result: 12
        // Result: 12345
        // Result: 12
        // Result: 4
        //
无法将类型“bool”转换为“int”
        // 编译错误,不能将string转化为int

       public static void TransformByIntParse()
        {
            try
            {
               Console.WriteLine("str {0}:", int.Parse(str));
               Console.WriteLine("str1 {0}:", int.Parse(str1));
               Console.WriteLine("str2 {0}:", int.Parse(str2));
               Console.WriteLine("str3 {0}:", int.Parse(str3));
            }
            catch(Exception ex)
            {
               Console.WriteLine(ex.Message.ToString());
            }
        }

       // 值不能为空。
        //
输入字符串的格式不正确。
        // Result: 123
        // 输入字符串的格式不正确。

       public static void TransformByConvert()
        {
            try
            {
               Console.WriteLine("{0}", Convert.ToInt32(str));
               Console.WriteLine("{0}", Convert.ToInt32(str1));
               Console.WriteLine("{0}", Convert.ToInt32(str2));
               Console.WriteLine("{0}", Convert.ToInt32(str3));
            }
            catch(Exception ex)
            {
               Console.WriteLine(ex.Message.ToString());
            }
        }

        // Result: 0
        //
输入字符串的格式不正确。
        // Result: 123
        //
输入字符串的格式不正确。

       public static void Main(string[] args)
        {
           TransformByInt();
           TransformByIntParse();
           TransformByConvert();
        }
    }
}

ps
:C#不会对数据进行四舍五入,只会截取。


原文地址:https://www.cnblogs.com/call-from-dream/p/6238275.html