0

Possible Duplicate:
Advantages to Using Private Static Methods

In a project that i'm currently working on I found a private static class definition. It was part of a baseclass that derived from Page. The class contains some public static methods which are used in some baseclass methods.

As the original developer of this piece of code is gone, I wonder what the benefit is. The methods only return enum values.

example:

public class BasePage : Page
{
   protected override OnInit(EventArgs e)
   {
     ...
   }

   private static class SomeClass
   {
      public static myenumtype GetCategory(int id)
      {
           switch(id)
           {
                case 1: return myenumtype.one;
                case 2: return myenumtype.two;
                default: return myenumtype.zero;
           }
      }
   }
}
Community
  • 1
  • 1
Marcel de Kleine
  • 146
  • 1
  • 11
  • 1
    Well without code it's hard to state exactly what benefits that style is granting you. It could have been something as simple as just encapsulating a lot of related properties into a type that is only needed by the containing class. Or he could have been bored and couldn't be bothered to think of a better way of doing something. – Adam Houldsworth Oct 23 '12 at 09:36
  • A static class can only derive from Object, so your statement 'I found a private static class definition. It was part of a baseclass that derived from Page' seems flaky. – Justin Harvey Oct 23 '12 at 09:37
  • @Adam: Good point, but if he was really bored why not created a private method. A class seems a bit bloated. – Marcel de Kleine Oct 23 '12 at 11:13
  • @ChrisBint: Interesting notion; perhaps the writer thinks this performs better. don't know if it does however... – Marcel de Kleine Oct 23 '12 at 11:14
  • @Justin: the class was part of the derived class; see the code I added – Marcel de Kleine Oct 23 '12 at 11:15

3 Answers3

2

If you mean a nested class, it might simply be to "namespace" the methods. Rather than have a load of static helper methods cluttering up Intellisense in the parent class, this way you can "group" methods with related functionality together.

Rawling
  • 49,248
  • 7
  • 89
  • 127
  • Sounds fair, but it's the only one in the whole project (200K+ lines of code) and I don't know why he didn't choose a private method of the BasePage class instead. – Marcel de Kleine Oct 23 '12 at 11:16
  • If it's just the one method as in your code sample it doesn't make much sense. If it's multiple methods as in your first paragraph, then the point about namespacing still stands. – Rawling Oct 23 '12 at 11:19
1

This was probably done for encapsulation.

Having a private static class sounds like a helper class - it would make the code in the using class (the base class you are talking about) more concise and meaningful and take some incidental complexity into the private class.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Could be, but I think that would not be a sound solution to the cluttering problem. An extra helper class or a private static method instead of a private static class would be more logical and cleaner imo. – Marcel de Kleine Oct 23 '12 at 11:18
1

Most a private static class with public static methods is a Extensions-Class which provides functionality you use in your code without making this functionality a member of your Page.

You don't want this functionality to be accessed from outer assemblies.

Read this for further information:

Jan P.
  • 3,261
  • 19
  • 26
  • 1
    Although this would be nice, extension methods actually have to be defined in a top-level static class rather than a nested class. (Assuming we're talking about a nested class here. Otherwise - good point! :) ) – Rawling Oct 23 '12 at 09:40