Nested classes. Nested static classes. Declaring and using nested classes. Examples
Contents
- 1. The concept of nested class. The general form of the nested class declaration
- 2. What is the general form of the declaration of an object of the nested class?
- 3. What access modifiers can be applied to nested classes?
- 4. An example that demonstrates the use of classes with different access modifiers
- 5. Declaring and using a static nested class in a non-static class. Example
- 6. Is it possible to declare a non-static nested class in a static class?
- 7. Example of declaring and using a static nested class in another static class
- Related topics
Search other websites:
1. The concept of nested class. The general form of the nested class declaration
In C#, any class in its implementation may contain a declaration of another class. A class that is declared within curly brackets of another class is called a nested class.
In the simplest case, the general form of declaring an inline class in a class is:
// class that contains the implementation of another class declaration class Outer { // fields and methods of Outer class // ... class Inner { // fields and methods of Inner class // ... } // fields and methods of Outer class // ... }
where
- Outer – the name of the class that contains in its body a declaration of another class with name Inner;
- Inner – the name of the class that is declared in the Outer class.
The Outer and Inner classes can contain various specifiers that define access (private, protected, public) or other class properties (static, sealed, etc.).
⇑
2. What is the general form of the declaration of an object of the nested class?
You can declare a nested class object when the nested class is declared as visible. This means that the visible nested class must be declared with any access modifier other than private.
If the nested class is declared as a non-private class, then the creation of an instance of this class is as follows:
Outer.Inner objInner = new Outer.Inner();
where
- Outer – the name of the external class in which the nested class Inner is declared;
- Inner – the name of the nested class that is declared within the curly brackets of the Outer class;
- objInner – the name of the object (instance) of the class that is being created;
- Outer.Inner() – the name of the default constructor that is called to create a class object. If other parameterized constructors are implemented in the Inner class, this call may differs.
⇑
3. What access modifiers can be applied to nested classes?
The same access modifiers can be applied to nested classes as to ordinary non-nested classes:
- private. In this case, the nested class is visible within curly brackets { } of the outer class;
- public. In this case, the nested class is accessible within the outer class, from the outer class instance, the inherited class, and also outside the assembly;
- protected. In this case, the class is available within the outer class as well as from the methods of the inherited classes;
- internal. The nested class is available within the assembly and is not available outside the assembly;
- protected internal. The nested class may be accessible from external class methods, inherited class methods, or not accessable from methods outside the current assembly.
The effect of modifiers is exactly the same as in the case of data members (methods) of a class.
⇑
4. An example that demonstrates the use of classes with different access modifiers
The example demonstrates declaring and accessing to nested classes that contain various access modifiers.
The Outer class is declared, which includes:
- two internal public-variables with names d and sd. Variable sd is declared as static;
- internal method GetInner1(), that demonstrate the access to private-class Inner1;
- the nested class Inner1, which is declared as private;
- the nested class Inner2, which is declared as public;
- the nested class Inner3, which is declared as internal;
- the nested class Inner4, which is declared as protected;
- the nested class Inner5, which is declared as protected internal.
In any of the nested classes are declared:
- a non-static variable of type int;
- a static variable of type int.
The program code for the Outer class is as follows:
// class Outer contains 5 nested classes
public class Outer
{
// internal variables of class Outer
public int d;
static public int sd;
// access to the private class Inner1 from the internal class method
public Inner1 GetInner1()
{
Inner1 i1 = new Inner1(); // create an instance of Inner1 class
// access to the data member of the class Inner1 using an class instance
i1.d1 = 25;
// access to the static member of internal class Inner1
Inner1.sd1 = 30;
return i1;
}
// private nested class Inner1
private class Inner1
{
public int d1;
public static int sd1;
}
// public nested class Inner2
public class Inner2
{
public int d2;
public static int sd2;
}
// internal nested class Inner3
internal class Inner3
{
public int d3;
public static int sd3;
}
// protected nested class Inner4
protected class Inner4
{
public int d4;
public static int sd4;
}
// protected-internal nested class Inner5
protected internal class Inner5
{
public int d5;
public static int sd5;
}
}
The use of nested classes in a method may be as follows:
// Using of nested classes Outer.Inner1, Outer.Inner2, Outer
// 1. Declare an object of class Outer
Outer o = new Outer();
o.d = 300; // access to an internal variable using an instance of the class Outer
Outer.sd = 230; // access to static variable
// 2. No access to Inner1 private class via Outer instance
// Outer.Inner1 i1; - error, the Inner1 class is declared as private
// 3. Declare an object of public-class Inner2
Outer.Inner2 i2 = new Outer.Inner2();
i2.d2 = 440;
Outer.Inner2.sd2 = 500; // access to static variable
// 4. Declare an object of internal-class Inner3
Outer.Inner3 i3 = new Outer.Inner3();
i3.d3 = 100;
Outer.Inner3.sd3 = 400; // access to static variable
// 5. You can declare a protected-class object only from inherited classes.
// Outer.Inner4 i4; - error
// 6. Declare the object of protected-internal-class
Outer.Inner5 i5 = new Outer.Inner5();
i5.d5 = 200;
Outer.Inner5.sd5 = -100;
As can be seen from the above code, you can create objects only for classes Inner2, Inner3, Inner5. To create an object of protected-class Inner4, you need to declare another class that inherits the Outer class, for example:
// class Outer2 is inherited from class Outer
class Outer2 : Outer
{
// in the method of the inherited class there is access to the protected class Inner4
void SomeMethod()
{
// create an instance of Outer.Inner4 class
Inner4 i4 = new Inner4(); // it works
i4.d4 = 23; // access through a class instance
Inner4.sd4 = 330; // access to static variable sd4
}
}
This example also shows that the nested private-class hides access to the static variable declared in it from the methods of other classes. Also the nested protected-class hides access.
⇑
5. Declaring and using a static nested class in a non-static class. Example
In a non-static class, a static class (with the static keyword) can be declared. This means that the static class of this class is a unique shared resource. The elements of the static class are accessed directly by the name of the static class, before which, after the dot, follows the name (s) of the outer class (s).
Example. The class Outer is declared, which contains the declaration of the nested static class Inner. The Inner class declares a static internal public variable. The text of the class Outer is as follows:
// class Outer is non-static
public class Outer
{
// internal variables of Outer class
public int d;
static public int sd;
// static internal class Inner
public static class Inner
{
// Only static variables can be declared in a static class.
public static int sd; // static internal variable of Inner class
}
}
Using a static nested class might be something like this:
// access to the static variable of the static class Outer.Inner
Outer.Inner.sd = 45;
// create an object (instance) of Outer class
Outer o = new Outer();
o.d = 30; // there is access to class instance variables using an object
Outer.sd = 102; // access to static variable of class Outer
// you cannot create a static class object
//Outer.Inner i = new Outer.Inner(); - it's error!
⇑
6. Is it possible to declare a non-static nested class in a static class?
Yes, it is. In a static class, you can declare non-static nested classes. However, non-static variables and methods cannot be declared in a static class.
In more detail about the use of static variables, methods and classes are described in the topic:
For example. Let the static class Outer be declared, containing the implementation of the non-static class Inner
// class Outer is static
public static class Outer
{
// internal variables of Outer class
// Only static instances can be declared in a static class.
// public int d; - error!
static public int sd;
// non-static internal class Inner - it can be declared in a static class
public class Inner
{
public int d; // non-static variable
public static int sd; // static internal variable of Inner class
}
}
Using the classes Outer and Outer.Inner can be, for example, like this:
// use of non-static class in a static class
// access to static variable sd of class Outer.Inner
Outer.Inner.sd = 230;
// access to static variable sd of class Outer
Outer.sd = 132;
// It isn't possible to create an instance (object) of a static class.
// Outer o = new Outer(); // error, because Outer is static class
// create an instance of the nested class Outer.Inner
Outer.Inner i = new Outer.Inner();
і.d = 323; // access to non-static variable d of class Outer.Inner
⇑
7. Example of declaring and using a static nested class in another static class
In a static class, another nested static class can be declared. It should be remembered here that in static classes the declared methods and variables must be static. But classes can be non-static.
Let the declarations of the static classes Outer and Inner be given:
// class Outer - it is static class
public static class Outer
{
// Only static instances can be declared in a static class.
static public int sd;
// static nested class Inner - can be declared in a static class
public static class Inner
{
// here must be static members only
public static int sd; // static nested variable of Inner class
}
}
Then, the use of static members of these classes can be as follows:
// the use of static class in another static class
// access to static variable sd of Outer.Inner class
Outer.Inner.sd = 140;
// access to static variable sd of Outer class
Outer.sd = 110;
// You cannot create an instance (object) of a static class.
// Outer o = new Outer(); // error, because Outer is static class
⇑
Related topics
- Classes. Class definition. Object of class. Keyword this
- Nested classes. Nested static classes. Declaring and using nested classes. Examples
⇑