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.
Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property.
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)
An interface can contain declarations of methods, properties, indexers, and events. However, it cannot contain instance fields.
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; }
}
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