ASP.NET Interview questions and answers for Nested types

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 as nested class.

using System;
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?
using System;
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 bcos 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.

using System;
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.

using System;
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 ouput of the following program?
using System;
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:
I am a container class
I am an inner class
I am a Demo class

The above program has used the concepts of inheritance and nested classes. The ContainerClass is at the top in 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.

No comments:

 
Top Blogs