Visual Studio IDE0059 C#不必要的值错误分配? [英] Visual Studio IDE0059 C# Unnecessary assignment of a value bug?

查看:117
本文介绍了Visual Studio IDE0059 C#不必要的值错误分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下C#代码(为了简化起见,我将其减少到最低限度).Visual Studio 2019,.NET Framework 4.7.2.

I have the following C# Code (I reduced it to the bare minimum to simplify it). Visual Studio 2019, .NET Framework 4.7.2.

public void Demo()
{
    ReportStart();
    var success = false;
    try
    {
        int no = 1;
        switch (no)
        {
            case 1:
            default:
                break;
        }

        DoSomething();

        success = true;
    }
    finally
    {
        ReportEnd(success);
    }
}

据我了解,这没有错.该函数可能会失败(我不想捕获它),但是在离开之前,它将向另一种方法报告成功执行.调试时,它确实会执行应有的操作.

From my understanding, there is nothing wrong about it. The function may fail (I don't want to catch it) but before leaving, it will report successful execution to another method. When debugging, it does exactly what it should.

有趣的是,Visual Studio 2019将报告以下内容:

Interestingly, Visual Studio 2019 will report the following:

当我通过选择删除冗余分配"来遵循建议时,它将删除行 success = true; ,有效地改变了结果!

When I follow the suggestion by choosing "Remove redundant assignment", it will remove the line success = true;, effectively changing the outcome!

现在,您要问开关/保护套是做什么用的?删除该建议后,该建议会消失:

Now what is the switch/case for, you'd ask? When removing it, the recommendation disappears:

是否有任何原因,或者这是Visual Studio中的错误?

Is there any reason for that, or is it a bug in Visual Studio?

推荐答案

这似乎是Roslyn和Visual Studio 2019 16.4的已知问题,请参阅GitHub问题#39344 .

It seems to be a known issue with Roslyn and Visual Studio 2019 16.4, please refer to the GitHub issues #39755 and #39344.

该里程碑已设置为16.5 Preview 2版本,因此已经修复,可以尝试使用16.5版本的Preview 2或等待稳定版本(个人而言,我没有使用Preview版本)

The milestone is set to the version 16.5 Preview 2, so it was already fixed and you can try the preview 2 of 16.5 version or wait for stable one (personally, I'm not using a Preview versions)

这篇关于Visual Studio IDE0059 C#不必要的值错误分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆