Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...