c# - 冗余控制流跳转语句

标签 c#

我正在查看一些代码。 VS将return语句标记为Redundant control flow jump statement,建议去掉。正确的语法是什么?

private async void TokenButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            var accountType = _settings["account_type"];

            if (accountType.Equals(AccountTypeMicrosoft))
            {
                this.Status.Text += "The original token is good for Live. No new token is needed.\n";
            }
            else
            {
                // Get access token for the target service
                if (!await GetAccessTokenForServiceAsync().ConfigureAwait(true))
                {
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            this.Status.Text += "Exception caught: '" + ex.Message + "'.";
            this.Status.Foreground = _errorBrush;
        }
    }

最佳答案

删除整个 if 并将其替换为:

await GetAccessTokenForServiceAsync().ConfigureAwait(true)

您不需要检查结果,因为在任何一种情况下发生的下一件事都将是该方法的结束。

关于c# - 冗余控制流跳转语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26708375/

相关文章:

c# - Url.RouteUrl 在 ASP.NET Core 3.0 中为空

c# - 简单的 C# Metro StreamSocket 示例

c# - 如何在 C# 中制作/设计一个在运行时可以具有不同类型成员的类

c# - 使用 iTextSharp 获取包含在指定区域中的文本事件

c# - 直接打印时 GhostScript 挂起

C# 属性限制

c# - 添加单元测试项目时同名文件或文件夹已存在

c# - BeginInvoke 未在 WPF 应用程序的线程内触发

c# - WPF中使用 "Binding with StaticResource"和使用 "StaticResource directly"有什么区别

c# - 在 LoadDataRow 期间使 DataGridView 中一行中的所有单元格加载为粗体?