Implementing interface with auto properties

Akos Nagy
Nov 10, 2017

A cool feature in VS2017: when implementing an interface, you can now use auto properties.

Here's what I'm talking about: let's say you have an interface, and you have properties in it. Something like this:

public interface IInterface
{
  int MyProperty { get; set; }
}

Then, you want to implement this interface in a class. You type in the class name, add the interface, hit Ctrl+. and here's what you end up with:

public class Class : IInterface
{
  get => throw new NotImplementedException();
  set => throw new NotImplementedException();
}

And that's not what you need 95% of the time. Because 95% of the time, you want simple auto properties. So you can go back and delete the throw expressions. Messy.
In previous version of Visual Studio, you could find the right snippet, edit it, and you were done. But from VS2015 onward, the IDE uses Roslyn (the .NET Compiler Platform) to do all the refactorings, so the snippet-magic does not work.

I just found this handy little option in my Visual Studio (Enterprise Edition, version 15.4.1), which is a life saver, and does exactly what I want: instead of throwing properties, auto properties are generated when implementing interfaces:

Nice :)

Akos Nagy
Posted in Visual Studio C#