C# nested classes (vs Java)
Saturday, November 17th, 2007C# does support nested classes, but have some different than Javas’ inner classes.
Java ’s nested classes have several types: static nested class, inner class, local inner class(declare an inner class within the body of a method), annonymous inner class (declare an inner class within the body of a method without naming it).
Java inner class:
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
http://java.sun.com/docs/books/tutorial/java/javaOO/summarynested.html
A class defined within another class is called a nested class. Like other members of a class, a nested class can be declared static or not. A nonstatic nested class is called an inner class. An instance of an inner class can exist only within an instance of its enclosing class and has access to its enclosing class’s members even if they are declared private.
C# nested classes are like C++ nested classes, not Java inner classes (http://blogs.msdn.com/oldnewthing/archive/2006/08/01/685248.aspx)
When you declare a class inside another class, the inner class still acts like a regular class. The nesting controls access and visibility, but not behavior. In other words, all the rules you learned about regular classes also apply to nested classes.
In C++ and C#, you will have to implement this effect manually. It’s not hard, though:
// Java class OuterClass { string s; // ... class InnerClass { public InnerClass() { } public string GetOuterString() { return s; } } void SomeFunction() { InnerClass i = new this.InnerClass(); i.GetOuterString(); } }// C# class OuterClass { string s; // ... class InnerClass { OuterClass o_; public InnerClass(OuterClass o) { o_ = o; } public string GetOuterString() { return o_.s; } } void SomeFunction() { InnerClass i = new InnerClass(this); i.GetOuterString(); } }
C# seemed don't have Java's anonymous inner class. The reason why it don't have seemed to because it doesn't need, here is from a comment:
re: C# nested classes are like C++ nested classes, not Java inner classes
Tuesday, August 01, 2006 12:18 PM by JavaSharp
I suspect the reason this is in Java and not in C# is because C# has delegates, whereas Java must rely on inner classes (specifially, “anonymous inner classes”)
Here is a part from “C# Java differeces“:
3. Inner classes
In Java,
1. Private members of an outer class are accessible from the inner class.
2. There’s a concept of enclosing instance
3. Static inner classes were actually akin to top level classesIn C#,
1. Inner classes are called Nested Types
2. Private members of outer class are accessible from the nested class but
the enclosing instance must be passed to the nested class.
3. In fact, C#’s Nested classes are akin to Static inner classes of Java
(but even then you have to access members of the outer classes thru either an instance
or class name in case of a static member)4. Anonymous Classes
There are no anonymous classes in C#
5. Classes inside Methods
Not available in C#
C# does support anonymous delegates to archieve simpilar simplicity as Java’s anonymous class in many cases.
Codeproject: Inside C# 2.0 Anonymous Methods http://www.codeproject.com/csharp/InsideAnonymousMethods.asp
The implementation of anonymous methods in C# and its consequences (part 1)(http://blogs.msdn.com/oldnewthing/archive/2006/08/02/686456.aspx)
Visual Studio 2008 - Support for anonymous methods and lambda expressions (http://blogs.msdn.com/fxcop/archive/2007/09/21/new-for-visual-studio-2008-support-for-anonymous-methods-and-lambda-expressions.aspx)
The power of closures in C# 2.0 http://joe.truemesh.com/blog//000390.html :
Martin Fowler (obligitary Fowlbot namedrop) recently blogged about the power of closures in languages that support them. It’s worth remembering that C# 2.0 has true closure support in the form of anonymous delegates. This includes reading and modifying variables outside the context of the closure - unlike Java’s anonymous inner classes.
In this blog post he wrote some codes in Ruby and C#2.0, the C# code is simple enough however still not neat enough to beat Ruby, but I guess in C# 3.0, with the latest lambda expression, I think those C# code will be simpler than ruby.
BTW: C++ does support nested classes, and C++ classes can actually be defined inside a member function or whereever. C++ also have “anonymous class” concept, but means classes without a specific name, which is much different than what we discussed here.
About