Visual Studio 2017"实现接口"命令生成C#5代码?

时间:2017-04-04 14:06:54

标签: c# visual-studio visual-studio-2017

在Visual Studio 2017中,在" 快速操作和重构"菜单,当我点击" 实现接口"时,我得到的实现使用了C#的新功能,如下所示:

public int ConnectionTimeout => throw new NotImplementedException();

问题是我的公司正在使用C#5,因此我们无法使用这种新语法。我知道Visual Studio的旧版本会生成此代码:

public int ConnectionTimeout { get { throw new NotImplementedException(); } }

有没有办法让Visual Studio 2017执行此操作?我已经将我的项目设置为使用C#5编译。

编辑:我注意到代码生成器不一致。如果接口具有仅具有getter的bool成员,则它使用旧语法。否则它使用新语法。这让我觉得我运气不好。

private interface myint
{
    bool bool1 { get; }
    bool bool2 { get; set; }
    bool bool3 { set; }

    int int1 { get; }
    int int2 { get; set; }
    int int3 { set; }

    string string1 { get; }
    string string2 { get; set; }
    string string3 { set; }
}

private class myclass : myint //I clicked "Implement Interface" on this
{
    public bool bool1
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    public bool bool2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public bool bool3 { set => throw new NotImplementedException(); }

    public int int1 => throw new NotImplementedException();

    public int int2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public int int3 { set => throw new NotImplementedException(); }

    public string string1 => throw new NotImplementedException();

    public string string2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public string string3 { set => throw new NotImplementedException(); }
}

1 个答案:

答案 0 :(得分:8)

转到工具 - >选项 - >文字编辑器 - >代码风格 - >常规,滚动到代码块首选项部分并更改首选项对于属性对于索引器对于访问者“首选表达式块”(默认)到“首选块体”

enter image description here