Click here to Skip to main content
13,351,731 members (41,612 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 17 Aug 2012

Make your code Speak by using C# Region

, 17 Aug 2012
Rate this:
Please Sign up or sign in to vote.
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 : )
    • 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. 


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


About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

Hey, I am Pranay Rana, working as a ITA in 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 -
My CV :-


You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Fredrik Bornander26-Aug-13 21:43
memberFredrik Bornander26-Aug-13 21:43 
GeneralMy vote of 1 Pin
OriginalGriff17-Aug-12 4:20
mvpOriginalGriff17-Aug-12 4:20 
GeneralRe: My vote of 1 Pin
Andreas Gieriet17-Aug-12 5:08
memberAndreas Gieriet17-Aug-12 5:08 
GeneralRegions shouldn't be used to conceal poor coding practices Pin
John Brett17-Aug-12 4:14
memberJohn Brett17-Aug-12 4:14 
GeneralMy vote of 4 Pin
Farhan Ghumra17-Aug-12 4:09
memberFarhan Ghumra17-Aug-12 4:09 
GeneralOh, no! Don't encapsulate long code blocks into regions! Pin
Andreas Gieriet17-Aug-12 2:38
memberAndreas Gieriet17-Aug-12 2:38 
GeneralRe: Oh, no! Don't encapsulate long code blocks into regions! Pin
PIEBALDconsult17-Aug-12 8:51
memberPIEBALDconsult17-Aug-12 8:51 
GeneralRe: Oh, no! Don't encapsulate long code blocks into regions! Pin
Andreas Gieriet17-Aug-12 20:12
memberAndreas Gieriet17-Aug-12 20:12 
GeneralRe: Oh, no! Don't encapsulate long code blocks into regions! Pin
PIEBALDconsult18-Aug-12 5:24
memberPIEBALDconsult18-Aug-12 5:24 
QuestionMake your code Speak by using C# Region Pin
Lionel REPELLIN17-Aug-12 1:38
memberLionel REPELLIN17-Aug-12 1:38 
You can use NArrange to organize your regions automatically. You can modify the xml config file to adapt the behaviour of Narrage at your project.

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 17 Aug 2012
Article Copyright 2012 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid