Click here to Skip to main content
15,910,877 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Could you explain the purpose of the
C#
#region

    // ...

#endregion
directives.

I'm learning C# and I'm trying to ensure I observe best practices with the help of this site.
Posted
Updated 14-May-10 23:31pm
v2

They are used to group related sections of code so that multiple lines can be collapsed into a single line.

The best way to group is debatable. Many people (myself included) use the following groups (plus others)
Constants
Events
Fields
Constructors
Properties
Methods

Others group items that relate to each other e.g.
A field
The property that exposes the field
The event that is raised when the field is changed.

A combination of these can be used by nesting regions. Here's a full class that has a comprehebsive implementation of an int - cool. Without regions, with many fields/properties it can be hard to locate the correct regions in code. Now all Cool stuff is in one region, and nested regions for multiple methods etc.
C#
public class MyClass
{
    #region Cool Stuff

    #region Cool Constants

    /// <summary>
    /// The minimum Cool value.
    /// </summary>
    public const int CoolMin = 0;
    /// <summary>
    /// The maximum cool value.
    /// </summary>
    public const int CoolMax = 100;

    #endregion

    /// <summary>
    /// Raised when Cool changes.
    /// </summary>
    public event EventHandler CoolChanged;

    /// <summary>
    /// The internal value of Cool.
    /// </summary>
    private int cool;

    /// <summary>
    /// Gets or sets Cool.
    /// </summary>
    public int Cool
    {
        get { return cool; }
        set
        {
            if (cool != value)
            {
                if (CoolValidation(value))
                {
                    cool = value;
                    OnCoolChanged(EventArgs.Empty);
                }
                else
                    throw new ArgumentOutOfRangeException(
                        "Cool",
                        string.Format("Cool must be between {0} and {1} inclusive", CoolMin, CoolMax));
            }
        }
    }

    #region Cool Methods

    /// <summary>
    /// Checks whether the specified value is between CoolMin and CoolMax.
    /// </summary>
    /// <param name="cool">The value to test.</param>
    /// <returns>true if cool is between CoolMin and CoolMax; otherwise false.</returns>
    private static bool CoolValidation(int cool)
    {
        return cool >= CoolMin && cool <= CoolMax;
    }
    /// <summary>
    /// Raises the CoolChanged event.
    /// </summary>
    /// <param name="e"></param>
    protected virtual void OnCoolChanged(EventArgs e)
    {
        EventHandler eh = CoolChanged;
        if (eh != null)
            eh(this, e);
    }

    #endregion

    #endregion
}
 
Share this answer
 
Yes of course, but not this way. You need to use article section of this site to learn more about different topics.
This is Q&A section and people with specific programming issues ask questions here.

Abhinav Kumar Gupta wrote:
I want to learn C# in a better way


It's good that you are willing to learn. I would recommend buying a good C# book and start reading that. Also download Visual Studio Express Edition from Microsoft website. It is free. You can write and test your code with that. Moreover there are lots of tutorials and articles on CodeProject and on the web which will help you learn things better.

Lastly, but most importantly, learn how to use search engines (Google/Bing). For example, you wanted to know "what is #region in C#".
Click this[^] link.

Good luck and happy programming!
 
Share this answer
 
v2
Comments
Henry Minute 15-May-10 9:46am    
I consider the OPs question to be a perfectly valid question for Q&A. Your answer has some good advice, particularly the searching selection, but if the OP is new to programming he would not necessarily know the learning resources available.
Ankur\m/ 17-May-10 0:29am    
Hi Henry, thanks for the advice.
I answered the question before it was edited by Dave. If you check the unedited question, the OP was asking that he would like to learn C# and if we could help him here. So I was guiding him through the resources available on CodeProject and on the web. Please check the original question and let me know if I am wrong in my answer. I would surely consider your suggestion.
(I don't know if you would get a notification for this, but if you could read it, please let me know your views).
Dear,

This option (#region) is to collapse and expand a specific part of code, see below link for more details:

http://msdn.microsoft.com/en-us/library/9a1ybwek(VS.71).aspx

Thanks
 
Share this answer
 
hello,
#region is used to encapsulate text in the text editor

Thanks & Regards
Radix :)
 
Share this answer
 
In Simple sentence,
Region is used to manage the visibility of code. I liked this feature in Visual studio, very much.

Consider that your C# file having lots of code and logics so Region can be used to distinguished and mark different logical areas.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900