Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement interface properties NOT in alphabetical order

I use VS 2015.
When I create a new class and try to implement an interface with the shortcut Ctrl + . -- For example, class StarShip : IStarShip and then I use Ctrl + . and select implement interface -- It implements the properties and methods but in an alphabetical order! Is there an easy way to make it implement them in the order in which they are in the interface, or must I do it manually?

I don't have Resharper, so that's not an option.

like image 231
IdeasAreBulletProof Avatar asked Dec 05 '15 10:12

IdeasAreBulletProof


People also ask

How do you implement property in an interface?

In the interface, there is no code. 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.

Can interface has property 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.

Can I define a 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.

Should interfaces have properties?

Show activity on this post. Yes, An interface should define properties when it really in need.


2 Answers

In VS2019 you can change this in the options.

  1. Tools -> Options -> Text Editor -> C# -> Advanced -> Implement interface or Abstract class set to at the end.

  2. Restart Visual Studio.

This seems to add the functions in the same order as in the interface.

like image 188
HKrg Avatar answered Sep 23 '22 18:09

HKrg


The VS 'implement interface' functionality obviously uses reflection to generate the code, and there is nothing in the reflection APIs that guarantees the order of members returned by the various calls. We do not have control over either the reflection order or the VS code generator used to implement interfaces, so we can't change the way it works.

Basically the only option is to use a VS extension like resharper that replaces the implement interface functionality.

like image 28
Corey Avatar answered Sep 23 '22 18:09

Corey