tags:

views:

281

answers:

6

Very often it happens that I have private methods which become very big and contain repeating tasks but these tasks are so specific that it doesn't make sense to make them available to any other code part.

So it would be really great to be able to create 'inner methods' in this case.

Is there any technical (or even phylosophical?) limitation that prevents C# from giving us this? Or did I miss something?

+9  A: 

Well, we can have "anonymous methods" defined inside a function (I don't suggest using them to organize a large method):

void test() {
   Action t = () => Console.WriteLine("hello world");  // C# 3.0+
   // Action t = delegate { Console.WriteLine("hello world"); }; // C# 2.0+
   t();
}
Mehrdad Afshari
+1, this is exactly what you want.
Blindy
@Mehrdad Afshari: That looks interesting, it seems "Func" could be used for this. But why wouldn't you use them to organize a large method?
Marc
@Marc: You should consider breaking down a large method to several smaller methods, not anonymous methods.
Mehrdad Afshari
I break down large methods into anonymous methods fairly frequently, for two reasons. First, as the OP says, sometimes the sub-method is an implementation detail only of the outer method, and therefore can be encapsulated by it. Second, it means that I can use locals of the outer method in the inner methods without having to pass them around.
Eric Lippert
@Eric: I assume you do this in "functional-language style", not just breaking down as if it's a separate `#region`. Is that true?
Mehrdad Afshari
Briefly, yes. I have an example in mind, but it won't fit in a comment. Might make a good blog article some day. :-)
Eric Lippert
@Eric: I guessed so. I don't think overusing anonymous methods and using them all the time when one should have refactored to `private` methods is a good idea -- it's worth mentioning since your statement may be misinterpreted as such.
Mehrdad Afshari
+5  A: 

If something is long and complicated than usually its good practise to refactor it to a separate class (either normal or static - depending on context) - there you can have private methods which will be specific for this functionality only.

Grzenio
+2  A: 

I know a lot of people dont like regions but this is a case where they could prove useful by grouping your specific methods into a region.

CodeSpeaker
That's what I already do :-) I use regions and partial classes a lot.
Marc
+1  A: 

If your method becomes too big, consider putting it in a separate class, or to create private helper methods. Generally I create a new method whenever I would normally have written a comment.

Arkain
Creating a private helper method is what I did until know, but I usually end with so many private helper methods that some time later I don't know which private method uses which private method.That's why I'd like to include these helper methods within the method which requires them.
Marc
@Marc, if you method becomes too big it may be time to consider creating a new class, but it's hard to say what the "right" approach is without knowing the code.
Arkain
+2  A: 

Could you give a more concrete example? After reading your post I have the following impression, which is of course only a guess, due to limited informations:

  • Private methods are not available outside your class, so they are hidden from any other code anyway.
  • If you want to hide private methods from other code in the same class, your class might be to big and might violate the single responsibility rule.
  • Have a look at anonymous delegates an lambda expressions. It's not exactly what you asked for, but they might solve most of your problems.

Achim

Achim
I agree that lambda expressions could solve my problem. Even though using Func I can only pass 1 parameter. Mehrdad Afshari replyed that he wouldn't use this to simplify methods though...
Marc
A: 

The better solution is to refactor this method to separate class. Create instance of this class as private field in your initial class. Make the big method public and refactor big method into several private methods, so it will be much clear what it does.

Paul Podlipensky