Click here to Skip to main content
15,868,340 members
Articles / Programming Languages / C#
Tip/Trick

Make your code Speak by using C# Region

Rate me:
Please Sign up or sign in to vote.
2.72/5 (9 votes)
16 Aug 2012CPOL2 min read 73.1K   3   10
Make your code Speak by using C# Region

Post for the beginner developer or for the developer who want their code to speak and easy to understand to other developer who is going to maintain the code, review the code etc. 

To achive this thing most of the people talks about commenting class,method, property etc.but no one talks about the making region in the code which make code compact and easy to understand in form of regions. Region allow to block of the code which you can expand and minimize.

Syntax to define the region in you code is  

#region Properties
//your code
#endregion Properties

So above code defines the region with name Properties. What ever code you write between between region and endregion get minimize and expand in visual studio you can see the + when minimize and - when maximize the code block.

Example of the file with the region related to BAL or DAL layer of code  :


Example of Aspx file i.e presentation layer file


Why to use Region in your code
  • It allow to group the member which is related to same functionality 
    • For Example in above code : Insert/Update method , Get Method
  • It to group Class members (private, public, methods, properties, etc.) , which you can easily see in above image
  • With the help of region long code blocks can be minimize to small block, which you can expand when you want to see the code and minimize when work done with it.
SortCut keys and  Context menu for Region
  • Shortcut key for  region is (MSDN link for this : http://msdn.microsoft.com/en-us/library/td6a5x4s.aspx )
    • CTRL+M CTRL+M (that's two key presses!) - collapse/open the current parent region
    • CTRL+M CTRL+L - Collapse/Open all regions in document recursively (meaning you might get only one line in the document - one big namespace region which is collapsed or you'll see the entire page code uncollapsed
    • CTRL+M CTRL+O - Collapse all regions not recursively
  • You can also make use of Context menu for that in you text editor

When not to use Region in your code
  • To Hide 'ugly code' that is more complicated.
  • To hide misc. code so that lazy reviewers don't look and ask about that part of code.
  • Make region to hide the Commented code. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog


StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



Comments and Discussions

 
GeneralMy vote of 1 Pin
Fredrik Bornander26-Aug-13 20:43
professionalFredrik Bornander26-Aug-13 20:43 
GeneralMy vote of 1 Pin
OriginalGriff17-Aug-12 3:20
mveOriginalGriff17-Aug-12 3:20 
GeneralRe: My vote of 1 Pin
Andreas Gieriet17-Aug-12 4:08
professionalAndreas Gieriet17-Aug-12 4:08 
I think you misread the last section. He's against hiding ugly code too, etc.
Unfortunately, he advises to hide long code - which is really bad advise, I think.
Cheers
Andi
GeneralRegions shouldn't be used to conceal poor coding practices Pin
John Brett17-Aug-12 3:14
John Brett17-Aug-12 3:14 
GeneralMy vote of 4 Pin
Farhan Ghumra17-Aug-12 3:09
professionalFarhan Ghumra17-Aug-12 3:09 
GeneralOh, no! Don't encapsulate long code blocks into regions! Pin
Andreas Gieriet17-Aug-12 1:38
professionalAndreas Gieriet17-Aug-12 1:38 
GeneralRe: Oh, no! Don't encapsulate long code blocks into regions! Pin
PIEBALDconsult17-Aug-12 7:51
mvePIEBALDconsult17-Aug-12 7:51 
GeneralRe: Oh, no! Don't encapsulate long code blocks into regions! Pin
Andreas Gieriet17-Aug-12 19:12
professionalAndreas Gieriet17-Aug-12 19:12 
GeneralRe: Oh, no! Don't encapsulate long code blocks into regions! Pin
PIEBALDconsult18-Aug-12 4:24
mvePIEBALDconsult18-Aug-12 4:24 
QuestionMake your code Speak by using C# Region Pin
Lionel REPELLIN17-Aug-12 0:38
Lionel REPELLIN17-Aug-12 0:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.