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; } }
The NotImplementedException exception indicates that the method or property that you are attempting to invoke has no implementation and therefore provides no functionality. As a result, you should not handle this error in a try/catch block. Instead, you should remove the member invocation from your code.
On implementation of an interface, you must override all of its methods. Interfaces can contain properties and methods, but not fields/variables. Interface members are by default abstract and public. An interface cannot contain a constructor (as it cannot be used to create objects)
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With