Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring "Implement Interface" using auto properties VS2015

I am trying to get Visual Studio 2015 (14.0) to use auto properties when implementing an interface using refactoring for C#.

I.e. I want this;

public object SomeProperty { get; set; }

as opposed to this;

public object SomeProperty
{
    get
    {
        throw new NotImplementedException();
    }
    set
    {
        throw new NotImplementedException();
    }
}

I have accomplished this in past versions of Visual Studio by editing the code snippet file (instructions here) but I cannot get this to work using Visual Studio 2015.

like image 953
angus Avatar asked Sep 02 '15 22:09

angus


2 Answers

Ok, so I stumbled upon the answer during my testing of VS2019 Preview (16.0).

In the main menu bar Tools --> Options --> Text Editor --> C# --> Advanced look for the option Implement Interface or Abstract Class under When generating properties choose prefer auto properties.

This results in the same outcome that snippets used to take care of pre VS2015.

like image 53
angus Avatar answered Nov 05 '22 12:11

angus


You can solve by editing the PropertyStub.snippet

Just go to C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Refactoring open PropertyStub.snippet and edit:

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

to

$GetterAccessibility$ get;
$SetterAccessibility$ set;
like image 30
Mauro Sampietro Avatar answered Nov 05 '22 13:11

Mauro Sampietro