CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C# – Classes – Outer, Inner and Wrapper Classes

In this article we will look into few more things about the classes.

Outer Classes, Inner Classes and Wrapper Classes

‘C#’ classes allows to define classes (inner classes) within the classes. Inner classes are only accessible through its outer classes or wrapper classes. To create an instance of an Inner class, we have to create it through its outer class.

Wrapper classes are the classes which doesn’t allow to create an instance of its inner classes but allowed to access inner class’s members/methods through its methods.

Lets see the below code:

// Sample.cs
//
class OuterClass
{
    class PrivateClass
    {
        internal void Display()
        {
            System.Console.WriteLine("PrivateClass");
        }
    }

    internal class InnerClass
    {
        internal void Display()
        {
            System.Console.WriteLine("InnerClass");
        }
    }

    internal void Display()
    {
        System.Console.WriteLine("OuterClass");
    }

    internal void DisplayPrivateClass()
    {
        PrivateClass obj = new PrivateClass();
        obj.Display();
    }
};

class Sample
{
    static void Main()
    {
        OuterClass outer = new OuterClass();

        outer.Display();
        outer.DisplayPrivateClass();

        OuterClass.InnerClass inner = new OuterClass.InnerClass();

        inner.Display();
    }
}

From above code, OuterClass is a class which contains InnerClass and PrivateClass. OuterClass is behaves like a wrapper class to its inner class PrivateClass by allowing to access PrivateClass members/methods only through OuterClass methods.

InnerClass is a class which is defined in OuterClass and OuterClass allowed to access InnerClass members/methods. Observe that InnerClass instance is created through its outer class OuterClass only.

Why internal used in front of class or its members? This is an access-specifier to restrict the class access and its members access to assembly level only. That means classes in other assemblies are not able to access the classes and its members which are declared with “internal” access specifier.

An Empty Class

In ‘C#’, we can create an empty classes also. Empty class is nothing but, a class have no members defined within it. Surprisingly ‘C#’ allows to create an instance of an empty class. Lets see below example:

// Sample.cs
//
class EmptyClass
{
};

class Sample
{
    static void Main()
    {
        EmptyClass empty = new EmptyClass();

        System.Console.WriteLine(empty);
    }
}

Observe that, we have successfully created an instance for an empty class. But how instance got created without a Constructor? As we discussed in our previous article, ‘C#’ provides a default constructor when no constructor is specified. Hence we were able to create an instance for empty class.

Class objects are reference types

‘C#’ class objects are reference types. They are not value types. For example: if you want to create an “int” variable, you could write the code as:

int i;

But to create a ‘C#’ class variable, you should not write the code as:

<class name> obj;

Where <class name> is the name of the class for which you want to create an instance. From above line of code, ‘C#’ creates a memory for “obj”. This is a memory for reference. But where it points to? Its not pointing to anything. Now, if you try to use “obj” variable, ‘C#’ will throw an error:

error CS0165: Use of unassigned local variable 'obj'

Because it is a reference variable, and not pointing to anything; and we are using the variable in the code; hence ‘C#’ throws the error.

The resolution is, we have to create a class instance using ‘new’ operator and assign its memory to ‘obj’ variable. After this, ‘obj’ reference variable points to an instance of a class; through which ‘C#’ can able to retrieve members of a class.

Lets discuss more about classes in our next articles…

..

C# – Classes – Outer, Inner and Wrapper Classes

One thought on “C# – Classes – Outer, Inner and Wrapper Classes

  1. In the given C# code, there are two types of classes mentioned: InnerClass and PrivateClass. How do these classes differ in terms of access and instantiation, and what role does the OuterClass play in enabling access to these inner classes?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top