Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort Fields, Properties and Methods in Visual Studio?

I know that for clarity in codes, I have to write first fields then constructor and then methods etc. But how can I automate it in Visual Studio?

like image 964
Freshblood Avatar asked Jul 24 '10 18:07

Freshblood


People also ask

How do I view properties in Visual Studio?

You can find Properties Window on the View menu. You can also open it by pressing F4 or by typing Properties in the search box. The Properties window displays different types of editing fields, depending on the needs of a particular property.

Where is properties in Visual Studio code?

You access project properties by right-clicking the project node in Solution Explorer and choosing Properties, or by typing properties into the search box on the menu bar and choosing Properties Window from the results. .

How do I sort alphabetically in Vscode?

Select my lines of text. Press F9. Keep your eyes on the code, you'll see a change. And now it's in alphabetical order.


2 Answers

I think CodeMaid is the best free option for code formatting in Visual Studio.


To sort your file, open the file via solution explorer:

  1. Right click the open file
  2. Code Maid menu (likely near the top of the right click menu)
  3. Click Reorganize Active Document

Alternatively, using the default CodeMaid hotkeys CTRL+M,Z to sort your active file.

like image 160
Alan Feekery Avatar answered Sep 18 '22 15:09

Alan Feekery


ReSharper can sort your class members (fields, constructors, methods, delegates) by name, accessibility, type, readonly, etc... You can also surround specific members with regions. What I like the most is the ability to group interface members (e.g., #region IDisposable with void Dispose() method in it) and methods that handle an event.

ReSharper provides both - an easy way to configure and trigger the sorting of class members.

Configuration

Create a XML file within Visual Studio and copy-paste the default type member layout (ReSharper Options > Languages > C# > Type Members Layout) into that file. Download the latest XSD schema. Add the schema file to Visual Studio's schema files (Menu > XML > Schemas... > Add). You should be able now, to edit the XML file with the help of IntelliSense.

Triggering

If you use the Visual Studio keyboard scheme (ReSharper Options > Visual Studio Integration) and press Ctrl+E,F for Silent Code Cleanup. A dialog will pop up, where you can select a Code Cleanup setting. For this setting you should check Reorder type members. The second time you press the shortcut, ReSharper will automatically reorder your class members.

1: ReSharper Type Members XSD Schema

like image 36
Matthias Avatar answered Sep 17 '22 15:09

Matthias