18

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.

2
  • 6
    #region Constants .... #endregion ? Commented Feb 21, 2014 at 17:18
  • 2
    Wow, I was really amazed by the speed you guys answer questions here on SO, when I came back in 5 minutes and saw all the answers and votes, I had the illusion that I've already slept for a night after asking the question and just woke up.
    – Ascendant
    Commented Feb 21, 2014 at 17:31

4 Answers 4

41

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.

1
  • 7
    The only thing I would add is that you should put the name of the region on the #endregion directive (i.e. #endregion Constants). That way if the region is a little long and you're at the bottom you know where you are.
    – Craig W.
    Commented Feb 21, 2014 at 17:26
7

You can use #regions.

#region lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor.

public class Foo
{

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

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

}
3

You can use #regions but there are some disadvantages associated with it like people tend to hide dirty code under it or hide some long eye crying method.

I am fine with your approach also but basically write a code in such a way that you don't need to hid under regions.

If you will make it readable then don't need separators at all....

1
  • I fervently disagree with your last comment. Regions are not (and should not be) about readability, but about organizing your code and working on the regions you're interested in while not displaying the rest of the file.
    – Franz B.
    Commented Jul 27, 2017 at 15:08
2

I tried using #region, but I didn't like how using Ctrl + M, O (Collapse to Definitions hotkey) would collapse regions so that I couldn't easily see which functions / members were in each one. I tried using block comments, but those would collapse as well.

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.