在哪兒使用 C# 中的 #region 指令?


它允許你指定一組程式碼,使用視覺化工作室程式碼編輯器中的概覽功能可以展開或摺疊這些程式碼。應用 #endregion 可終止這些程式碼。

讓我們看看如何使用 #region 定義一個區域。

#region NewClass definition
public class NewClass {
   static void Main() { }
}
#endregion

下面是一個展示如何使用 #region 指令的示例。

示例

 線上演示

using System;
#region
class MyClass {
}

#endregion
class Demo {
   #region VARIABLE
   int a;
   #endregion
   static void Main() {
      #region BODY
      Console.WriteLine("Example showing the usage of region directive!");
      #endregion
   }
}

輸出

Example showing the usage of region directive!

更新於: 22-Jun-2020

4 千+ 瀏覽量

職業起步

完成課程認證

開始使用
廣告
© . All rights reserved.