Nested Types Interview Questions C#.NET

Nested Types Interview Questions and Answers C#.NET

In this article, I am going to discuss most frequently asked Nested Types Interview Questions and Answers on C#.NET.

What is a nested type? Give an example?

A type (class or a struct) defined inside another class or struct is called a nested type. An example is shown below. InnerClass is inside ContainerClass, Hence InnerClass is called a nested class.

namespace Nested
{
    class ContainerClass
    {
        class InnerClass
        {
            public string str = "A string variable in nested class";
        }

        public static void Main()
        {
            InnerClass nestedClassObj = new InnerClass();
            Console.WriteLine(nestedClassObj.str);
        }
    }
}
Will the following code compile?
namespace Nested
{
    class ContainerClass
    {
        class InnerClass
        {
            public string str = "A string variable in nested class";
        }
    }

    class Demo
    {
        public static void Main()
        {
            InnerClass nestedClassObj = new InnerClass();
            Console.WriteLine(nestedClassObj.str);
        }
    }
}

No, the above code will generate a compile time error stating – The type or namespace name ‘InnerClass’ could not be found (are you missing a using directive or an assembly reference?). This is because InnerClass is inside ContainerClass and does not have any access modifier. Hence inner class is like a private member inside ContainerClass. For the above code to compile and run, we should make InnerClass public and use the fully qualified name when creating the instance of the nested class as shown below.

namespace Nested
{
    class ContainerClass
    {
        public class InnerClass
        {
            public string str = "A string variable in nested class";
        }
    }

    class Demo
    {
        public static void Main()
        {
            ContainerClass.InnerClass nestedClassObj = new ContainerClass.InnerClass();
            Console.WriteLine(nestedClassObj.str);
        }
    }
}
Can the nested class access, the Containing class. Give an example?

Yes, the nested class or inner class can access the containing or outer class as shown in the example below. Nested types can access private and protected members of the containing type, including any inherited private or protected members.

namespace Nested
{
    class ContainerClass
    {
        string OuterClassVariable = "I am an outer class variable";

        public class InnerClass
        {
            ContainerClass ContainerClassObject = new ContainerClass();
            string InnerClassVariable = "I am an Inner class variable";
            public InnerClass()
            {
                Console.WriteLine(ContainerClassObject.OuterClassVariable);
                Console.WriteLine(this.InnerClassVariable);
            }
        }
    }
    class Demo
    {
        public static void Main()
        {
            ContainerClass.InnerClass nestedClassObj = new ContainerClass.InnerClass();
        }
    }
}
What is the output of the following program?
namespace Nested
{
    class ContainerClass
    {
        public ContainerClass()
        {
            Console.WriteLine("I am a container class");
        }
        public class InnerClass : ContainerClass
        {
            public InnerClass()
            {
                Console.WriteLine("I am an inner class");
            }
        }
    }

    class DemoClass : ContainerClass.InnerClass
    {
        public DemoClass()
        {
            Console.WriteLine("I am a Demo class");
        }
        public static void Main()
        {
            DemoClass DC = new DemoClass();
        }
    }
}

Output:

Nested Types Interview Questions and Answers C#.NET

The above program has used the concepts of inheritance and nested classes. The ContainerClass is at the top of the inheritance chain. The nested InnerClass derives from outer ContainerClass. Finally, the DemoClass derives from nested InnerClass. As all the 3 classes are related by inheritance we have the above output.

SUMMARY

In this article, I try to explain most frequently asked Nested Types Interview Questions and Answers on C#.NET. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Follow Us

Leave a Reply

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