14

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.

1

7 Answers 7

7

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”:

Selecting “Implement mebers”

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”:

Selecting “Missing members”

This will open Generate window, where you can select to implement the property (or properties) as auto-implemented:

Generate window

That will do exactly what you want:

class Employee : IEmployee
{
    public int Id { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you this was exactly what I am looking for. I program using tdd and am constantly refactoring/moving/adding/deleting items in my interfaces so manually adjusting the throw new NonImplemented() exception in tons of interfaces implemented in my classes was always so time consuming. I'm using resharper 6 and the resharper 2.x keyboard layout so your instructions worked fine. Thanks again for the response (and for everyones too)!
7

After all, I defined it as an auto implemented property in my interface.

No, you didn't. You declared it as a property without an implementation. That's all you can do in an interface: you're just saying that classes implementing the interface must provide the concrete implementations of such properties.

Personally I would be wary of having too many writable properties within interfaces - if this is something you find "happens all the time" I wonder whether you're using interfaces where possibly abstract classes would be more appropriate.

In terms of your exact question: I don't know whether it's possible to change the default implementation either VS or R# provides for interfaces - but I would resist making those changes anyway, to be honest.

EDIT: Under R# options, "Code Generation", you can choose between throwing an exception, returning a default value, or giving uncompilable code. It's possible that this will do what you want. Give it a go, but I'd still strongly urge you to think carefully before going down this path.

1 Comment

FYI, the "default value" option results in an expanded property that returns the default value for the type and has an empty setter.
3

An interface is not meant to specify how the methods will be implemented so there is no way around it using the interface. One solution would be to make an abstract base class with the auto-implemented properties and inherit that class instead of directly implementing the interface.

Comments

3

Here's a quick workaround that I found in VS2015. Mark your class as abstract then implement the interface abstractly. This adds the auto properties for you then you just replace the "abstract " with "" in your file. Then you can remove the abstract keyword from your class.

Comments

1

In the case of VS2015 I'm using a find and replace macro as a workaround:

Find:
(\r|\n| |\t)+\{(\r|\n| |\t)+get(\r|\n| |\t)+\{(\r|\n| |\t)+throw(\r|\n| |\t)+new(\r|\n| |\t)+NotImplementedException\(\);(\r|\n| |\t)+\}(\r|\n| |\t)+set(\r|\n| |\t)+\{(\r|\n| |\t)+throw(\r|\n| |\t)+new(\r|\n| |\t)+NotImplementedException\(\);(\r|\n| |\t)+\}(\r|\n| |\t)+\}

replace:
 {get;set;}

Comments

0

In addition to Jon's answer... if you really want to change this (out of box) behavior of Visual Studio and creating auto properties when implement interface, you can try one of following...

  1. Use T4 for code generation - http://msdn.microsoft.com/en-us/library/bb126445.aspx

  2. Write a custom plugin for Visual Studio using VS SDK achieve this

Comments

0

It's not related to this question completely, however, a different approach when you have multiple of such properties in an Interface, instead of interface you can have a class and refer to that class as a return type in your main class. You can create a class and refer to that class in your main class. Example

public class Asset
{
    public int AssetTrackingID { get; set; }

    public Category AssetCategoryInfo { get; set; }

    public Manufacturer AssetManufacturerInfo { get; set; }

    public ManufacturerModel AssetModelInfo { get; set; }

    public Status AssetStatusInfo { get; set; }

    public EmployeeDetails AssetEmployeeInfo { get; set; }

    public string AssetNumber { get; set; }

    public string SerialNumber { get; set; }

    public DateTime? AcquiredDate { get; set; }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.