在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(); }
}