Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

c# - Implement interface includes throw new NotImplementedException... why?

I'm using VS2017 Community and it just received an update yesterday. Today I wanted to implement an interface and now the implementation looks like this:

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

Instead of this (what I expected):

public string City { get; set; }

Why this change? Not sure if this is specific to C#7 or VS or whatever. I just know that the auto implementation of interfaces has changed over the last week or so.

My interface:

public interface IMyInterface
{
    string City { get; set; }
}
question from:https://stackoverflow.com/questions/43470473/implement-interface-includes-throw-new-notimplementedexception-why

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, I know this is an old question, but it looks like Visual Studio 2017 now allows you to choose between the two styles (throw vs plain old get;set;). (I'm using 15.7.5 of 2017 Community).

To do this, go to Tools=>Options=>Text Editor=>C#=>Advanced, and scroll to the bottom, where you should have an Implement Interface or Abstract Class section.

Inside of that, you can set the When generating properties radio button to either the default "prefer throwing properties", or to the old style by selecting "prefer auto properties".

Here's where to look for it in the Options setting: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...