Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make implementation of an interface produce an auto property instead of NotImplementedException?

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

Is there any way of providing just an auto property as seen on interface. This cause re-factoring issues and make me crazy each time!

    public interface ICar     {         double Power { get; set; }     }      public class Car:ICar     {          public double Power         {             get             {                 throw new NotImplementedException();             }             set             {                 throw new NotImplementedException();             }         }     } 
like image 209
Davut Gürbüz Avatar asked Jul 17 '13 14:07

Davut Gürbüz


People also ask

Can we create property in interface?

Beginning with C# 8.0, an interface may define a default implementation for members, including properties. Defining a default implementation for a property in an interface is rare because interfaces may not define instance data fields.

Can we create property in interface in C#?

In C#, an interface can be defined using the interface keyword. An interface can contain declarations of methods, properties, indexers, and events. However, it cannot contain fields, auto-implemented properties.

How do you initialize an interface property?

You just specify that there is a property with a getter and a setter, whatever they will do. In the class, you actually implement them. The shortest way to do this is using this { get; set; } syntax. The compiler will create a field and generate the getter and setter implementation for it.


1 Answers

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

t

like image 121
Norbert Rozmus Avatar answered Sep 23 '22 05:09

Norbert Rozmus