Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In C#, when I have different code sections like constants, API functions, helper functions, etc., I would like to divide them. I normally 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 divide them instead of using a bunch of ugly comments? Like in Objective-C you can use

#pragma mark - Inner Classes

I've looked at all the keywords in the pragma list of C#, and none of them looks promising.

like image 701
AsyncMoksha Avatar asked Feb 21 '14 17:02

AsyncMoksha


1 Answers

C# has regions which serve a similar function. To use regions, your code would look a bit 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
}

If you're using Visual Studio, the C# editor allows you to collapse regions, making it easier to browse large source files.

like image 150
p.s.w.g Avatar answered Nov 10 '22 08:11

p.s.w.g