#region指令的作用是什么? [英] What is the #region directive used for?

查看:209
本文介绍了#region指令的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能解释一下
的目的吗?

Could you explain the purpose of the

#region

    // ...

#endregion

指令.

我正在学习C#,并且试图确保在此网站的帮助下遵守最佳实践.

directives.

I''m learning C# and I''m trying to ensure I observe best practices with the help of this site.

推荐答案

它们用于将以下内容的相关部分分组代码,以便可以将多行折叠为一行.

最好的分组方式值得商bat.许多人(包括我自己)使用以下组(以及其他组)
常量
活动
字段
构造函数
属性
方法

其他人将彼此相关的项目分组,例如
领域
公开字段的属性
字段更改时引发的事件.

嵌套区域可以使用这些方法的组合.这是一个完整的类,具有完整的int实现-cool.没有区域,具有许多字段/属性,可能很难在代码中找到正确的区域.现在,所有Cool内容都在一个区域中,并且嵌套区域中有多种方法等.
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.
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
}


是的,但是不是这样.您需要使用本网站的文章部分,以了解有关不同主题的更多信息.
这是问答环节,有特定编程问题的人在这里提出问题.

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写道:
Abhinav Kumar Gupta wrote:

我想以更好的方式学习C#

I want to learn C# in a better way



愿意学习是一件好事.我建议您购买一本不错的C#书,然后开始阅读.还可以从Microsoft网站下载Visual Studio Express Edition.这是免费的.您可以使用它来编写和测试您的代码.此外,在CodeProject和网络上还有很多教程和文章,可以帮助您更好地学习.

最后,但最重要的是,学习如何使用搜索引擎(Google/必应).例如,您想知道"C#中的#region是什么".
点击 [ ^ ]链接.

祝你好运,编程愉快!



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!


亲爱的,

此选项(#region)用于折叠和扩展代码的特定部分,有关更多详细信息,请参见下面的链接:

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

谢谢
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


这篇关于#region指令的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆