简体   繁体   English

调试和发布之间的C#区别

[英]C# difference between Debug and Release

Following code produces same output on 95% of machines, but on several there is difference. 以下代码在95%的机器上产生相同的输出,但在几个机器上存在差异。 In Debug mode there is output: 在调试模式下有输出:

Changing from New to Fin
OK

but in Release mode: 但在发布模式下:

Changing from New to Fin

The OK line is missing. OK行丢失了。 The project is targeted to .Net 4.0, build with VS 2015. You can download full sample here . 该项目的目标是.Net 4.0,使用VS 2015构建。您可以在此处下载完整示例。

Source code 源代码

using System;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Status current = Values.Status;
            if (current != Next())
                Console.WriteLine("OK");
        }

        static VO Values = new VO();
        private static Status Next()
        {
            Status res = Status.Fin;
            if (Values.Status == Status.New && Values.Cond)
                res = Status.Fin;
            else if (Values.Status == Status.Fin)
                res = Status.Fin;

            Log("Changing from {0} to {1}", Values.Status, res);
            Values.Status = res;
            return res;
        }
        public static void Log(string format, params object[] args)
        {
            Console.WriteLine(format, args);
        }
    }

    public class VO
    {
        public Status Status;
        public bool Cond;
    }

    public enum Status { New, Fin }
}

This is in my opinion the minimal version to reproduce the error. 这是我认为重现错误的最小版本。 After removing some of the conditions in Next(), inlining Log method, replacing Values.Cond with false causes the application behaves correctly. 删除Next()中的某些条件后,内联Log方法,将Values.Cond替换为false会导致应用程序正常运行。

Edit : It's not hardware related - operating system was extracted to Hyper-V and the problem persists. 编辑 :它与硬件无关 - 操作系统被提取到Hyper-V并且问题仍然存在。

Based on Hans Passant's comment the problem was still reproducible with clrjit.dll version 4.6. 根据Hans Passant的评论, clrjit.dll版本4.6的问题仍然可以重现。 After upgrading to 4.7 it disappears. 升级到4.7后,它会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM