-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] Local class declaration #280
Comments
C# does support anonymous local class delcarations, which I would think solves any of the use cases a local class would solve. Intellisense does already pick up the member names, and it is statically typed, and assignment between objects of the same type work, so this only gives you the chance to define constructors (methods could already be assigned with lambdas) and that would not be appropriate for a locally declared class anyways. I can't see any use case that anonymous classes don't solve that this local class declaration does solve. Anonymous classes are much more elegant in that it doesn't require declaring and then using them, you can simply use them. |
@mirhagk I can give an example, there have been times when i need to provide an IComparer to a method. But since anonymous classes can't have methods and can't implement interfaces, I have to declare a traditional class to do the job. I would be happy enough to have some syntax :
or using expression bodies
|
Local types have already been proposed below though, personally, I have my reservations about them: Proposal: Nested local functions and type declarations #259 |
@AlgorithmsAreCool Okay well I think the right route to go then would be to let anonymous classes declare methods (lambdas) and implement interfaces. That would be the most lightweight solution to the problem. |
There are some other cases, especially in algorithm design. |
@AlgorithmsAreCool I think the right solution for that specific case is to use a library that converts key selector to
|
@svick I think there are many other interfaces that we want to implement anonymously and locally. Such a library solution is somewhat adhoc and may cause some performance issues. We cannot write a library for each of them. |
Duplicate of #259 |
java supports local class declaration, e.g.
public void Method1(){
class Class1 {
}
}
This is super useful when a function logic is complex, and some data types are not used outside the function. Such local class just solves the problem.
Besides, C++ also supports that.
It is a pity that C# does not support that feature.
And I think this is just a syntax sugar, and does not involve the changes of CLR. So it is an easy feature.
In java, the local class is just treated as an inner class, e.g. MethodName$LocalClassName.
And C# can use the same strategy.
The text was updated successfully, but these errors were encountered: