2

I'm using ReSharper and CodeMaid, both have their own "organize" code option but the problem is that i have no idea how to tell them what is an event and what is a method/function that i coded.

If i reorganize code (i want regions) it groups every control event and method together.

Is there a way to tell them what is a method and what is an event? or is there another extension that is specifically better for reorganizing code the way i want?

I want something like this:

private void button1_click(object sender, EventArgs e){}

private void MyMethod1(){}

private void MyMethod2(){}

private void button2_click(object sender, EventArgs e){}

to become:

#region Events
private void button1_click(object sender, EventArgs e){}

private void button2_click(object sender, EventArgs e){}
#endregion Events

#region Methods
private void MyMethod1(){}

private void MyMethod2(){}
#endregion Methods

closed as off-topic by user40980, Kilian Foth, GlenH7, Ampt, psr Sep 23 '14 at 19:54

  • This question does not appear to be about software engineering within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
  • jetbrains.com/resharper/webhelp/… – rwong Sep 23 '14 at 0:15
  • 1
    Don't use regions. You can achieve the organization properly by sorting methods alphabetically using resharper. But don't use regions. Regions are evil and annoying and deprecated. – Stephen Sep 23 '14 at 0:23
  • @Stephen: When have they been deprecated? Any source for this? – phresnel Sep 25 '18 at 7:01
  • I don't mean that it's been formally deprecated, but informally it was deprecated from the moment Partial classes were introduced into C#. Regions were created to hide auto-generated code, primarily in Winforms. With the invention of partial classes, all of this was able to be hidden in a separate file. We already have tools to group related code - classes and methods. We already have tools to add comments to code - comments. Partial classes fulfilled the last valid use for the #region directive. They will never be fully removed from the C# spec for compatibility reasons but don't use them. – Stephen Sep 26 '18 at 3:31
4

Regions shouldn't be used. Never.

If you want to organize methods, properties, fields, events, etc., you may be interested in following StyleCop rules and in making StyleCop checking mandatory during every commit.

Also, if you're currently writing code in a basic text editor, you may be interested in moving to Visual Studio, where methods, properties, fields, events, etc. have distinctive icons in Intellisense which make it very easy to recognize visually. Note that Visual Studio Express is free.

  • Often people use the excuse "sorting into regions makes it easier to get to the methods", but that's what the right hand drop down list above the code area is for. There is no good use case for regions since partial classes have been introduced to .NET. – Stephen Sep 23 '14 at 0:34
  • 1
    @Stephen - "sorting into regions makes it easier to get to the methods" - no, if your methods are hard to get to, your class is too complex. – Telastyn Sep 23 '14 at 0:47
  • I agree completely with you @Telastyn. – Stephen Sep 23 '14 at 0:50
  • Haha the only reason of why i want regions is so they can show up as "headers" in spade when using CodeMaid. – Hikaros Sep 23 '14 at 1:15
  • @Stephen: Partial classes should most definitely not be used for grouping. The fact that you'd say they make regions unnecessary is horrific in the extreme. I agree that you should not need regions, because you should keep your classes small, but partial classes should not come into this at all. – Magus Sep 23 '14 at 14:44
2

Is there a way to sort code properly into regions dividing methods and events?

No.

Is there a way to sort code properly divide methods and events?

Yes. Follow the resharper guidelines, sorting by accessibility and then alphabetically. You can then use ctrl+e, ctrl+c (code cleanup) to use resharper to sort them for you.

  • Makes me sad, i guess i'll go with it and StyleCop as suggested before :c thanks. – Hikaros Sep 23 '14 at 1:41
  • The other advantage of using stylecop is that it'll make your code easier to read for others and will get you used to "generic" c# code (i.e. make other people's code easier to read for you). – Stephen Sep 23 '14 at 2:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.