Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the #region directive really useful in .NET?

After maintaining lots of code littered with #region (in both C# and VB.NET), it seems to me that this construct is just a bunch of "make work" for the programmer. It's work to PUT the dang things into the code, and then they make searching and reading code very annoying.

What are the benefits? Why do coders go to the extra trouble to put this in their code.

Make me a believer!

like image 685
jm. Avatar asked Oct 03 '08 23:10

jm.


People also ask

What is a good song to dedicate to your child?

Suggestions for Special Songs to Dedicate to Your Child:Baby Mine by Alison Krauss. Beautiful Boy by John Lennon (there is also a Celine Dion version) The Best Part of Me by Lee Price. Blessed by Elton John.

Who created Cocomelon?

CoComelon was created in 2005 by Jay Jeon, a father of two in Southern California. Jeon, who had directed some TV commercials, was trying to teach his kids the ABCs. He started working with his wife, a children's-book author, to make videos to accompany the nursery rhymes they sang to their sons.


3 Answers

A similar question has already been asked.

but...

I would say not anymore. It was originally intended to hide generated code from WinForms in early versions of .NET. With partial classes the need seems to go away. IMHO it gets way overused now as an organizational construct and has no compiler value whatsoever. It's all for the IDE.

like image 94
Scott Saad Avatar answered Oct 27 '22 09:10

Scott Saad


Often times, both partials and #regions are used as a crutch for bad design (e.g. class is too big or tries to do too many things).

The best use I've had for #regions so far is the grouping of functionality that is seen in many different classes. For example, value objects that have getters, setters, constructors and supporting fields. I might very well group those ideas into regions. Its a matter of opinion, however, as to whether that makes code cleaner or harder to read.

like image 41
Russell Myers Avatar answered Oct 27 '22 09:10

Russell Myers


http://www.rauchy.net/regionerate/ - Automatically regionised your code ;)

I'm a fan of regions for grouping sections of large classes, say all the properties together, all constances, etc. I'm someone who's constantly collapsing code I don't need to see at that time so I love regions for that.

Also I find regions really useful when implementing interfaces, particularly multiple interfaces. I can group each interfaces methods, properties, events, etc so it's easier at a glance to see what method belongs to what interface.

like image 33
Aaron Powell Avatar answered Oct 27 '22 09:10

Aaron Powell