Proposal: Anonymous inner class #4148
Answered
by
YairHalberstadt
pengweiqhca
asked this question in
Language Ideas
-
Anonymous inner class
SummaryA type is only used once, and don't want to be named. public class TestClass
{
private void TestMethod()
{
MyInterface localVar = new MyInterface()
{
public void method1()
{
someCode();
}
public void method2(int i, boolean b)
{
someCode();
}
};
}
} The corresponding C# implementation: public class TestClass
{
private void TestMethod()
{
MyInterface localVar = new MyInterfaceAnonymousInnerClass(this);
}
private class MyInterfaceAnonymousInnerClass : MyInterface
{
private readonly TestClass outerInstance;
public MyInterfaceAnonymousInnerClass(TestClass outerInstance)
{
this.outerInstance = outerInstance;
}
public void method1()
{
someCode();
}
public void method2(int i, bool b)
{
someCode();
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
YairHalberstadt
Nov 18, 2020
Replies: 2 comments 8 replies
-
C# already supports anonymous types, but they can't define methods or implement interfaces, Java allow this because they don't have delegates, so this is their way to work around them, other than that, I don't see where this would be useful |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
YairHalberstadt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Duplicate of #738, #1542, #2298