How to create auto implemented properties when implementing an interface into a class?
c#, class, interface, properties
Solution
Note: your R# keyboard shortcuts may differ, I am using the Resharper 2.x keyboard schema.
If you declare the interface on the class and then use Alt+Enter and select “Implement members”:
Then you will get the default implementation, which happens to be throwing `NotImplementedException`, unless you change that.
But if you ignore the suggested course of action and instead use Alt+Insert to open the Generate menu, you can select “Missing members”:
This will open Generate window, where you can select to implement the property (or properties) as auto-implemented:
That will do exactly what you want:
class Employee : IEmployee
{
public int Id { get; set; }
}
Problem
When I implement an interface for the first time into a class I want either resharper 6 or visual studio 2010 to implement my properties as auto implemented properties and not put in the default value of throw new NonImplementedException();. How can I do this? For example: ``` public interface IEmployee { // want this to stay just like this when implemented into class ID { get; set; } } public class Employee : IEmployee { // I do not want the throw new NonImplemented exception // I want it to just appear as an auto implemented property // as I defined it in the interface public int ID { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } } ``` Because this happens all the time, I am finding myself having to constantly refactor and manually remove those throw new UnImplimented() exceptions and manually make the properties be auto implemented... a pain! After all, I defined it as an auto implemented property in my interface. Any help or advice much appreciated! Thanks.