Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

06 June 2012

Nesting of Classes in C#.NET

In this article we will discuss about nesting of classes in C#.NET with examples.

“A nested class is one which is created inside another class.” A class which is created in another called referred as nested.

Syntax:
class OutsideClass
    {
        // members of outsideclass
        class InsideClass
        {
            // members of outsideclass
        }
    }

Example:
class NestingOfClasses
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("/********** Nesting of classes example **************/");
            OutsideClass objOutside = new OutsideClass();
            objOutside.ShowMessage();
            //InsideClass objInside = new InsideClass(); //Error : The type or namespace name 'InsideClass' could not be found (are you missing a using directing or an assembly reference?)
            OutsideClass.InsideClass objInside = new OutsideClass.InsideClass(); //2nd Level innner class           
            objInside.ShowMessage();
            OutsideClass.InsideClass.InnerInsideClass objInside1 = new OutsideClass.InsideClass.InnerInsideClass()//3rd Level innner class           
            objInside1.ShowMessage();
            Console.Read();
        }
    }
    class OutsideClass
    {
        // members of outsideclass
        public void ShowMessage()
        {
            Console.WriteLine("Invoked from outsideclass");
        }
        public class InsideClass
        {
            // members of InsideClass
            public void ShowMessage()
            {
                Console.WriteLine("Invoked from InsideClass");
            }
             public class InnerInsideClass
             {
                 // members of InnerInsideClass
                 public void ShowMessage()
                 {
                     Console.WriteLine("Invoked from InnerInsideClass");
                 }
             }
        }
    }
 
Output:


Data points:
  • If we try to create object directly for nested classes we will get error as “The type or namespace name 'InsideClass' could not be found (are you missing a using directing or an assembly reference?)”.
  • By default nested class access modifiers is Private. So mark nested class with public otherwise we can’t access them in the program. We will get error as “’namespace.classname’ is inaccessible due to protection levels
  • A nested class can have private, protected and protected internal access modifiers along with public and internal where as normal should be public or internal.
  • Nested classes can access outside class object's private members even if they are not static.
  • Nested class can access the static members of the containing outer class without using that classes’ name. Whereas for outer class to access static members of nested class then it should be like NestedClassName.staticdatamembername.
Accessing data members in nested/parent classes:
If we create data members in parent/nested class and try to access them in nested/parent class it is not possible.
class OutsideClass
    {
        public string strParent = "I am string strParent  from outside class";
// We can’t access strNested  in this class
 
        class InsideClass
        {
        public string strNested  "I am string strNested  from Inside class";
 
// We can’t access strParent  in this class
        }
    }

So in order to access data members of parent/nested classes in nested/parent class we can do in two ways
1.       Marking the data members as “static” – see example1
2.       Creating objects in the classes – see example2

Example 1: Accessing using Data members as “static”:

 class OutsideClass
    {
        // members of outsideclass
        public static string strParent = "I am string strParent from outside class";
        public void ShowMessage()
        {
            Console.WriteLine(InsideClass.strNested);
            
        }
        public class InsideClass
        {
            
            // members of InsideClass
            public static string strNested = "I am string strNested from InsideClass class";
            public void ShowMessage()
            {
                Console.WriteLine(OutsideClass.strParent);               
                
            }
            
        }
    }
  class NestingOfClasses
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("/********** Accessing Data members of parent/nested classes example **************/");
            OutsideClass objOutside = new OutsideClass();
            objOutside.ShowMessage();
            OutsideClass.InsideClass objInside = new OutsideClass.InsideClass();             objInside.ShowMessage();
Console.Read();
        }
    }
 
 
Output:


Example2: Accessing data members using object creation:

    class OutsideClass
    {
        // members of outsideclass
        public string strParent = "I am string strParent from outside class";
        public void ShowMessage()
        {
            InsideClass objInsideClass = new InsideClass();
            Console.WriteLine(objInsideClass.strNested);
            
        }
        public class InsideClass
        {
            
            // members of InsideClass
            public string strNested = "I am string strNested from InsideClass class";
            public void ShowMessage()
            {
                OutsideClass objOutsideClass = new OutsideClass();
                Console.WriteLine(objOutsideClass.strParent);               
                
            }
            
        }
    }
class NestingOfClasses
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("/********** Accessing Data members of parent/nested classes example **************/");
            OutsideClass objOutside = new OutsideClass();
            objOutside.ShowMessage();
            OutsideClass.InsideClass objInside = new OutsideClass.InsideClass();             objInside.ShowMessage();
Console.Read();
        }
    }
 
 
Output:


Use of nesting classes:
  • Data hiding can be achieved with nesting of classes. We can hide the members/details of inner class to the outside world. 
  • Easy way of logically grouping classes that are only used in one place.

0 Comments:

Post a Comment