Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Nested Procedures


I'm new to C#, coming from Delphi.  There doesn't seem to be any way to nest a procedure inside of another. This is something I made a lot of use of in my Delphi code.

As far as I can tell, I have 3 options:
  1) Just ad the methods to the class;
  2) Replace the method in which I would like to have nested methods with a new class; or
  3)  define a new class within the method.

Any thoughts?   

Corporate Dork
Monday, February 23, 2004

Coming from Delphi myself, I'll offer my opinion here ...

I have always avoided nested procedures.  The only time I thought they made sense was for pre and post validation.  Even then, I tended to refactor them into their own class.  Or exposed them as class methods.  I use the same rule for C#  (static methods instead of class methods of course).

If the methods actually have logic in them, refactor them into a private method within the class.

Canuck
Monday, February 23, 2004

Whidbey will bring anonymous methods (inline declaration of short methods) but I don't think C# will ever have true nested methods. They kind of went out style after Pascal, and they aren't really necessary since you can just declare a method private in the same class, and have it nearly as nicely encapsulated.

Chris Nahr
Tuesday, February 24, 2004

Canuck - thanks. I'm getting used to separate classes with static methods.  At first I thought it would be too much, but I'm finding the new classes make the code better organized. 

Corporate Dork
Monday, March 08, 2004

*  Recent Topics

*  Fog Creek Home