Inner classes in C #

Until recently, I did not believe that there was a difference between a normal class and an inner class / subclass.

What is the relationship between an instance of an inner class and an instance of its containing class, and what is the purpose of the inner classes / what makes them different?

+4
source share
4 answers

Unlike Java, classes containing C # are nested. There is no relation between the contained instance of the class and the instance of the contained class. The contained classes are used only in C # to control the accessibility of the contained class and to eliminate polluting namespaces.

(Some companies have a coding standard that each class should include in its own file; the classes contained are a path that is suitable for small classes.)

In Java, an instance (object) of an inner class returns a pointer to an outer class. This was done in Java, as it uses many small classes to handle events, etc. C # has delegates for this.

(The classes contained were one of the experimental ideals in Java that everyone liked, but did not actually prove the test of time. Since C # came much later, he could learn from Java that it didn’t work out well)

+15
source

.NET has no inner classes like Java. It has nested classes.

The reason you use them is to control the accessibility of the class.

+6
source

In C #, there are at least 3 differences between regular classes and inner classes, which can also form a connection between the inner class and the outer class that contains it.

  • Inner classes can be declared protected, internal, protected internal or private, while ordinary classes cannot.
  • The inner class can access the static members of the containing outer class without using this cluster name.
  • When an inner class accesses an instance of an outer class, it can access the private members of this object, even if they are not static.
+4
source

C # - contained classes are nested. There is no relationship between an instance of the containg class and an instance of the contained class.

+1
source

All Articles