More elegant way to write code section separators in C #?

In C #, when I have different sections of code, such as constants, API functions, helper functions, etc., I would like to separate them. I usually use something like this:

public class Foo {

      //================== Constants ==================
      private const string VIP = "Cob H.";
      private const int IMPORTANT_NUMBER = 23; 

      //================== API Functions ==================
      [WebMethod(MessageName = "SomeInformation")]
      public string SomeInformation() {
            return VIP + " is dead.";
      }

      //================== Inner Classes ==================
      private class IrrelevantClass {
            public string Name { get; set; }
            public string City { get; set; }
      }
}

Is there an elegant way to separate them instead of using a bunch of ugly comments? As in Objective-C, you can use

#pragma mark - Inner Classes

I looked through all the keywords in a pragmatic list in C #, and none of them look promising.

+4
source share
4 answers

C # has areas that perform a similar function. To use regions, your code would look something like this:

public class Foo {

      #region Constants
      private const string VIP = "Cob H.";
      private const int IMPORTANT_NUMBER = 23; 
      #endregion

      #region API Functions
      [WebMethod(MessageName = "SomeInformation")]
      public string SomeInformation() {
            return VIP + " is dead.";
      }
      #endregion

      #region Inner Classes 
      private class IrrelevantClass {
            public string Name { get; set; }
            public string City { get; set; }
      }
      #endregion
}

Visual Studio, # , .

+10

#.

#region , Visual Studio .

public class Foo
{

    #region Constants
    private const string VIP = "Cob H.";
    private const int IMPORTANT_NUMBER = 23;
    #endregion

    //......rest of the code

}
+6

#regions, , , - .

, , .

, ....

+3

#region, , Ctrl + M, O (Collapse to Definitions hotkey) , , / . , .

I came up with my own high visibility, multi-line delimiter that doesn't break when using Collapse to Definitions. Perhaps someone will find this idea useful:

enter image description here

0
source

Source: https://habr.com/ru/post/1528159/


All Articles