Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
int.Parse是否在C#中使用装箱/取消装箱或类型转换?_C# - Fatal编程技术网

int.Parse是否在C#中使用装箱/取消装箱或类型转换?

int.Parse是否在C#中使用装箱/取消装箱或类型转换?,c#,C#,当我说 int a = int.Parse("100"); Prse方法中是否有装箱/拆箱或类型转换发生?我不相信有任何拆箱发生。我假设代码使用int变量并返回它,因此没有装箱/取消装箱 编辑:我同意280Z28。我只是看了一下代码,它非常复杂。最终的值并没有像我想象的那样被装箱,但是有一些冗长的预处理步骤,所以即使装箱,性能也不会有太大的变化 顺便说一句,如果您不知道,您可以自己查看代码。没有装箱或取消装箱。你为什么这么问?解析算法比box/unbox操作昂贵得多,因此即使存在性能差异,您

当我说

 int a = int.Parse("100");

Prse方法中是否有装箱/拆箱或类型转换发生?

我不相信有任何拆箱发生。我假设代码使用int变量并返回它,因此没有装箱/取消装箱

编辑:我同意280Z28。我只是看了一下代码,它非常复杂。最终的值并没有像我想象的那样被装箱,但是有一些冗长的预处理步骤,所以即使装箱,性能也不会有太大的变化


顺便说一句,如果您不知道,您可以自己查看代码。

没有装箱或取消装箱。你为什么这么问?解析算法比box/unbox操作昂贵得多,因此即使存在性能差异,您也不会“感觉到”性能差异。

是的。使用,您可以确切地看到发生了什么。我看不到任何拳击比赛,但有几个演员,例如:

private static unsafe void StringToNumber(string str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo info, bool parseDecimal)
{
    if (str == null)
    {
        throw new ArgumentNullException("String");
    }
    fixed (char* str2 = ((char*) str))
    {
        char* chPtr = str2;
        char* chPtr2 = chPtr;
        if (!ParseNumber(ref chPtr2, options, ref number, info, parseDecimal) ||
           ((((long) ((chPtr2 - chPtr) / 2)) < str.Length) && //cast
            !TrailingZeros(str, (int) ((long) ((chPtr2 - chPtr) / 2))))) //cast
        {
            throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
        }
    }
}
真正的问题是——那么?

再加上我的一点

“取消装箱”的术语/技术仅与装箱值类型相关。 因此,如果值类型变量未装箱(转换为引用类型),则可以取消装箱


在您的情况下,您有一个有效的引用类型值,它没有可以取消绑定的范围。

或者只看Mono的版本。第114行附近:
private static unsafe char* MatchChars(char* p, string str)
{
    fixed (char* str2 = ((char*) str)) //cast
    {
        char* chPtr = str2;
        return MatchChars(p, chPtr);
    }
}