Книга: C# 2008 Programmer

#region and #endregion

#region and #endregion

The #region and #region preprocessor directives are used in conjunction with Visual Studio's Code Editor. Let's work with the following example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDefine {
 class Program {
  static void Main(string[] args) {
   //---implement ions here---
  }
  private void Method1() {
   //---implementations here---
  }
  private void Method2() {
   //---implementations here---
  }
  private void Method3() {
   //---implementations here---
  }
 }
}

Often, you have many functions that perform specific tasks. In such cases, it is often good to organize them into regions so that they can be collapsed and expanded as and when needed. Using this example, you can group all the methods — Method1(), Method2(), and Method3() — into a region using the #region and #region preprocessor directives:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDefine {
 class Program {
  static void Main(string[] args) {}
#region "Helper functions"
  private void Method1() {
   //---implementations here---
  }
  private void Method2() {
   //---implementations here---
  }
  private void Method3() {
   //---implementations here---
  }
#endregion
 }
}

In Visual Studio 2008, you can now collapse all the methods into a group called "Helper functions". Figure 3-19 shows the Code Editor before and after the region is collapsed.


Figure 3-19 

The #region and #region preprocessor directives do not affect the logic of your code. They are used purely in Visual Studio 2008 to better organize your code.

Оглавление книги


Генерация: 1.340. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз