zoukankan      html  css  js  c++  java
  • 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#不会对数据进行四舍五入,只会截取。


  • 相关阅读:
    每天30分钟搞Software Rendering(0)搜集资料,阅读,了解
    [Flash开发笔记]如何让Flash的swf文件在客户端不缓存
    FMS 中文帮助 (下载)
    [Flash开发笔记] List控件删除指定label或data的项
    用C#截取指定长度的中英文混合字符串 改进版
    [Flash开发笔记] AS中保留多少位小数的函数
    [Flash开发笔记] ComboBox,List组件的二个函数
    [FMS开发笔记]理解应用程序实例(聊天室房间的划分)
    [Flash开发笔记] 系列
    FMS如何连接FlashRemoting?(附示例源码下载)
  • 原文地址:https://www.cnblogs.com/call-from-dream/p/6238275.html
Copyright © 2011-2022 走看看