System.Environment.NewLine是否真的为每次调用分配30个字节的垃圾?

时间:2016-07-17 04:30:01

标签: c# unity3d

Unity的分析器显示每次调用StringBuilder.AppendLine时都会分配30个字节的垃圾,因为它访问System.Environment.NewLine。

所以我的问题:

  1. Unity的分析器是否在这里给我正确的信息?
  2. NewLine的值是否可以在运行时实际更改,还是对于任何给定的平台都是有效的?
  3. 如果它无法更改,为什么框架在第一次访问属性时不会将值赋给静态变量?
  4. 编辑:我刚刚在Unity的mscorlib.dll上使用了IL spy并找到了该属性的来源:

    private static string nl;
    
    ...
    
    public static string NewLine
    {
        get
        {
            if (Environment.nl != null)
            {
                return Environment.nl;
            }
            Environment.nl = Environment.GetNewLine();
            return Environment.nl;
        }
    }
    
    ...
    
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern string GetNewLine();
    

    即使它在第一次通话时产生了一些垃圾,也不可能每次都做得更多。看起来好像是分析器是错误的,但它让我想知道如何发生这种情况。

0 个答案:

没有答案