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; }
}
Solution 1:
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:
Solution 2:
I personally hope this is a bug. At this point we can only guess why the team changed the behavior.
However, generally speaking, there is a good reason to implement 'failing' code by default: you, as a developer, has to deliberately make a decision on how to implement that piece of code. What if the default implementation of a method would just return default(T)
? The code will 'work' until someone notices the 'not implemented' code.
I would argue though that for properties you generally can say nowadays that auto-implemented properties are the way to go. In 99% of the occurrences, the default implementation is the correct one, in contrary to the above reasoning for methods.