43

Consider that we have a simple interface such as ICar when I move mouse over ICar expression and click on Implement Interface Visual Studio generates below implementation.

Is there any way of having just an auto property as seen in below sample. I believe this will improve implementation time, since most of the time auto property is the intended implementation.

    public interface ICar
    {
        double Power { get; set; }
    }

    public class Car:ICar
    {

        public double Power
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83
  • what do you mean by "for" auto-properties. Do you mean *instead of* auto-properties? `{get; set;}` on an interface isn't an auto-property. – CodesInChaos Jul 17 '13 at 17:49
  • @CodesInChaos I expect auto property for `class`, for implementation. I would expected just `{get;set;}` as seen on interface. As seen there http://msdn.microsoft.com/en-us/library/bb384054.aspx . Thanks for pointing this. – Davut Gürbüz Jul 17 '13 at 21:38
  • @CodesInChaos sorry I noticed the title of question lately. You are right.I've changed it. – Davut Gürbüz Jul 17 '13 at 21:45

4 Answers4

73

You can change it in options: Tools > Options > Text Editor > C# > Advanced and on the bottom you have

t

Norbert Rozmus
  • 840
  • 1
  • 7
  • 8
24

You need to change the template used by Visual Studio when you click on Implement Interface. The template is stored in the following location:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Refactoring

The template you need to change is called PropertyStub.snippet

NOTE: I would back up the existing snippet file before making changes so that you can easily revert if things do not go well.

The lines you need to update are:

$GetterAccessibility$ get 
{ 
    $end$throw new $Exception$(); 
}
$SetterAccessibility$ set 
{ 
    throw new $Exception$(); 
}

The lines should be changed to this:

$GetterAccessibility$ get;
$SetterAccessibility$ set;
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • 1
    @AdamHouldsworth - Yeah if you start looking through the `Refactoring` folder it is amazing how much is template driven in VS. – Karl Anderson Jul 17 '13 at 15:10
  • Although your template looks a little off, I don't think that'd generate an auto-property. – Adam Houldsworth Jul 17 '13 at 15:12
  • @AdamHouldsworth - ah thank you, thought I had ripped out the right part. Updated answer. – Karl Anderson Jul 17 '13 at 15:17
  • @KarlAnderson is is possible to do that same thing but just on my useraccount instead changing the file for all users of the system on which Visual Studio is running? –  Oct 12 '15 at 11:36
  • @FlorianNeiss - I am not aware of being able to set snippet templates per user, not saying it is not possible just not sure how that would be achieved. – Karl Anderson Oct 12 '15 at 12:25
  • @LuisPalacios It didn't work for me in VS 2015 either – Rhyous Jul 19 '16 at 21:52
  • Unfortunately MS changed the way refactoring works in VS2015 and they don't use there templates any more (that they are still around just baffles me). They use Roslyn based refactor engine. More info at https://connect.microsoft.com/VisualStudio/feedback/details/1857694/visual-studio-2015-implement-interface-snippet-not-working – Igor Mar 23 '17 at 12:39
  • 5
    For VS2K15/VS2K17, use Norbert Rozmus's answer below. – Chris Bush Feb 26 '18 at 18:35
  • @Igor -- MS dead-ended your link. Do you know where the updated info went? – rory.ap Jun 13 '18 at 12:51
  • @rory.ap yes I was able to find it here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/1ced3e0e-578e-4f91-8988-a22a07feccbe/visual-studio-2015-implement-interface-snippet-not-working?forum=lsextensibility – Igor Jun 14 '18 at 13:46
3

First, that's not an auto-property. If you want an auto-property, you have to remove what the compiler produced and replace it with

public double Power { get; set; }

That is an auto-property.

The compiler does that because it's the simplest thing that the compiler can do that produces code that will compile. I suppose that it could do auto-properties for properties on interfaces but then that introduces an inconsistency between how it handles methods (it will also generate NotImplementedException method stubs) and properties. That said, you can change this. It's handled in the snippets:

1) Open the snippets directory and find this file C:\Program Files\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Refactoring\PropertyStub.snippet

2) Modify the PropertyStub.snippet file, replacing the property stubs with

 <![CDATA[$signature$ { $GetterAccessibility$ get; $SetterAccessibility$ set; }]]>

You can do the same for MethodStub.snippet so that it produces an empty body.

Now, when you auto-implement an interface using Visual Studio, you'll end up with

 public double Power { get; set; }
jason
  • 236,483
  • 35
  • 423
  • 525
3

If people come in search trying to do this is Visual Studio 2015 its not possible to modify the refactoring snippets. After the update to Roslyn it looks like Visual studio never actually references those snippet files anymore and does it directly in Roslyn.

Dakota Kincer
  • 448
  • 3
  • 16