What are the fundamental differences between Java and C# in terms of inner/local/anonymous classes?
What are the fundamental differences between Java and C# in terms of inner/local/anonymous classes
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
C# doesn’t have the equivalent of Java inner classes – it only has nested types (like Java’s ‘static’ nested classes).
The access rules are slightly different – in Java, an outer class has access to its nested class’s private members, and vice versa. In C# the nested class has access to the outer class’s private members, but not the other way round.
C# doesn’t have anonymous inner classes like Java, but it does have anonymous methods and lambda expressions, which are a much cleaner way of achieving most of what anonymous inner classes are usually used for. The variable capture for the two mechanisms is different – see my article on closures for more details.