0

As far as I can see it is not possible.

But on the other hand it is not possible to create a function in a function in C# either; unless one tries, that is. These lambda functions behave like ordinary functions.
Proper structs are not possible according to this question on SO. The solution presented for structs doesn't declare a type but an anonymous one.
One can use Tuple<> to get type safety but it is a bit hard to understand the content of myCustomer.Item1.

Now neither functions nor structs are classes and I hope someone have invented a clever trick somewhere.

I would like something like:

public void MyMethod()
{
    class MyClass
    {
        ...
    }
    var x = new MyClass();
    ...
}
Community
  • 1
  • 1
LosManos
  • 7,195
  • 6
  • 56
  • 107
  • It is not possible to create a class inside a function, however using lambdas it is possible to define a function inside a function as the links you posted suggest. – Ron Beyer Dec 21 '15 at 21:57
  • What's the motivating example? What *problem* are you facing such that "oh, if I could just declare a class from within a function, this would be easily solved" is the correct reaction? – Damien_The_Unbeliever Dec 22 '15 at 09:05
  • @Damien_The_Unbeliever Instead of littering the class with helper classes I can keep them inside the method. I can also have two classes "Result" but with different signatures; since they are hidden inside the method. It is not a matter of 'easily solved' but one of information hiding and readability. – LosManos Dec 22 '15 at 11:50
  • But you're still not offering any specific examples here. Bear in mind that every feature starts at [minus 100 points](http://blogs.msdn.com/b/ericgu/archive/2004/01/12/57985.aspx). Java had a "need" for such classes because they didn't have delegates. – Damien_The_Unbeliever Dec 22 '15 at 17:58

0 Answers0