This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Private inner classes in C# - why aren't they used more often

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 1.40 KB  |  views: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class ClassA
  2. {
  3.    public MethodA()
  4.    {
  5.       <some code>
  6.       myObjectClassB.DoSomething(); // ClassB is only used by ClassA
  7.       <some code>
  8.    }
  9. }
  10.  
  11. public class ClassB
  12. {
  13.    public DoSomething()
  14.    {
  15.    }
  16. }
  17.        
  18. public class ClassA
  19.    {
  20.       public MethodA()
  21.       {
  22.          <some code>
  23.          myObjectClassB.DoSomething(); // Class B is only used by ClassM
  24.          <some code>
  25.       }
  26.  
  27.       private class ClassB
  28.       {
  29.          public DoSomething()
  30.          {
  31.          }
  32.       }
  33.    }
  34.        
  35. public class MyCleverEnum
  36. {
  37.     public static readonly MyCleverEnum First = new FirstCleverEnum();
  38.     public static readonly MyCleverEnum Second = new SecondCleverEnum();
  39.  
  40.     // Can only be called by this type *and nested types*
  41.     private MyCleverEnum()
  42.     {
  43.     }
  44.  
  45.     public abstract void SomeMethod();
  46.     public abstract void AnotherMethod();
  47.  
  48.     private class FirstCleverEnum : MyCleverEnum
  49.     {
  50.         public override void SomeMethod()
  51.         {
  52.              // First-specific behaviour here
  53.         }
  54.  
  55.         public override void AnotherMethod()
  56.         {
  57.              // First-specific behaviour here
  58.         }
  59.     }
  60.  
  61.     private class SecondCleverEnum : MyCleverEnum
  62.     {
  63.         public override void SomeMethod()
  64.         {
  65.              // Second-specific behaviour here
  66.         }
  67.  
  68.         public override void AnotherMethod()
  69.         {
  70.              // Second-specific behaviour here
  71.         }
  72.     }
  73. }
clone this paste RAW Paste Data