C# Convert.ChangeType和Convert.ToInt32之间的主要区别是什么?

C# Convert.ChangeType和Convert.ToInt32之间的主要区别是什么?,c#,C#,如果您知道要将字符串转换为Int32,那么Convert.ChangeType或Convert.ToInt32或int.Parse之间是否存在性能优势。我当然更喜欢其他任何一个电话 int.Parse和Convert.ToInt32(x)之间的主要区别在于Convert.ToInt32(null)返回0,其中asint.Parse(null)将引发异常。当然,int.Parse还可以让您更好地控制文化的使用 我非常怀疑一种方法比另一种方法在性能上有什么好处:我希望Convert.ToInt32调

如果您知道要将
字符串
转换为
Int32
,那么Convert.ChangeType或Convert.ToInt32或int.Parse之间是否存在性能优势。我当然更喜欢其他任何一个电话

int.Parse
Convert.ToInt32(x)
之间的主要区别在于
Convert.ToInt32(null)
返回0,其中as
int.Parse(null)
将引发异常。当然,
int.Parse
还可以让您更好地控制文化的使用

我非常怀疑一种方法比另一种方法在性能上有什么好处:我希望
Convert.ToInt32
调用
int.Parse
,而不是相反的方法-但是没有文件证明这种方法是有效的,而且单个方法调用的影响不大。(它很可能是内联的。)

是的

Convert.ToInt32优于出于相同目的使用Convert.ChangeType


ChangeType是一种通用转换方法,用于将value指定的对象转换为conversionType。而ToInt32特定于int32类型。

简单测试表明
Parse()
是最快的方法,接下来是
Convert.ToInt32()
和最后一个
Convert.ChangeType()

private const int maxValue = 1000000;
    static void Main(string[] args)
    {
        string[] strArray = new string[maxValue];
        for (int i = 0; i < maxValue; i++)
        {
            strArray[i] = i.ToString();
        }
        int[] parsedNums = new int[maxValue];
        CalcChangeTypePerf(strArray,parsedNums);
        CalcToInt32Perf(strArray, parsedNums);
        CalcIntParse(strArray, parsedNums);
    }
    public static void CalcChangeTypePerf(string[] strArray,int[] parsedArray)
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        for (int i = 0; i < maxValue; i++)
        {
            parsedArray[i] = (int)Convert.ChangeType(strArray[i], typeof(int));
        }
        stopwatch.Stop();
        Console.WriteLine("{0} on CalcChangeTypePerf", stopwatch.ElapsedMilliseconds);
    }
    public static void CalcToInt32Perf(string[] strArray, int[] parsedArray)
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        for (int i = 0; i < maxValue; i++)
        {
            parsedArray[i] = Convert.ToInt32(strArray[i]);
        }
        stopwatch.Stop();
        Console.WriteLine("{0} on CalcToInt32Perf", stopwatch.ElapsedMilliseconds);
    }
    public static void CalcIntParse(string[] strArray, int[] parsedArray)
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        for (int i = 0; i < maxValue; i++)
        {
            parsedArray[i] = int.Parse(strArray[i]);
        }
        stopwatch.Stop();
        Console.WriteLine("{0} on CalcIntParse", stopwatch.ElapsedMilliseconds);
    }

Convert.ToInt32()和Int32.Parse()都使用(不公开)方法System.Number.ParseInt32(),该方法反过来调用System.Number.StringTonNumber()。我怎么知道的?看看传递无效字符串时生成的FormatException的堆栈跟踪:-@user532870:Fair-但我仍然不相信这种事情:)@user532870:Jon是对的。反射显示Convert.ToInt32调用int.Parse,后者调用Number.ParseInt32@乔恩:那么int.Parse更好吗?@naveen:这取决于你想要的确切行为。如果您对使用当前区域性感到满意,并且希望它返回0表示null,请使用Convert.ToInt32。如果您想要int.Parse的行为,请使用:)您在每个测试中创建了一百万个对象,这是不必要的。虽然每个测试都是“相等”的,但这意味着实际解析时间的相对差异在某种程度上被掩盖了,并且可能在一个测试中进行垃圾收集,从另一个测试中清除垃圾。我建议您一开始就创建一个字符串数组并重用它。事实上,
ChangeType()
是编程的
cast
,同时
Convert.ToInt32()
真正从其他类型创建
int
不要忘记
Int32.TryParse(String,out int)
,这提供了一些很好的可能性。
266 on CalcChangeTypePerf
167 on CalcToInt32Perf
165 on CalcIntParse
static void Main(string[] args)
{
    string s = "104563";
    int a = 1;

    for (int k = 0; k < 4; k++)
    {
        Stopwatch w = Stopwatch.StartNew();
        for (int i = 0; i < 10000000; i++)
            a = (int)Convert.ChangeType(s, typeof(int));
        w.Stop();

        Console.WriteLine("ChangeType={0}", w.ElapsedMilliseconds);

        Stopwatch w1 = Stopwatch.StartNew();
        for (int i = 0; i < 10000000; i++)
            a = Convert.ToInt32(s);
        w1.Stop();

        Console.WriteLine("ToInt32={0}", w1.ElapsedMilliseconds);

        Stopwatch w2 = Stopwatch.StartNew();
        for (int i = 0; i < 10000000; i++)
            a = Int32.Parse(s);
        w2.Stop();
        Console.WriteLine("Parse={0}", w2.ElapsedMilliseconds);
    }

    Console.ReadLine();
}
ChangeType=2422
ToInt32=1859
Parse=1760
ChangeType=2374
ToInt32=1857
Parse=1762
ChangeType=2378
ToInt32=1860
Parse=1763
ChangeType=2375
ToInt32=1855
Parse=1759