Friday, December 23, 2011

Nested Classes in C#

C# supports the Nested Classes concept. Nested classes in C# are similar to Java’s static inner classes, but there is no concept like Java’s non-static inner classes in C#.

Please have a look into the dummy code given below :- 

Program.cs 

class Program
    {
        static void Main(string[] args)
        {
            OuterClass outerClass = new OuterClass();
            outerClass.DisplayPosition();

            OuterClass.InnerClass innerClass = new OuterClass.InnerClass();
            innerClass.DisplayPosition();

            OuterClass outerClassArg = new OuterClass("outer arg");
            outerClassArg.DisplayPosition();

            OuterClass.InnerClass innerClassArg = new OuterClass.InnerClass("inner arg");
            innerClassArg.DisplayPosition();

            Console.ReadKey();
        }
    }



NestedClassesInCSharp.cs 

/// <summary>
    /// Outer class
    /// </summary>
    public class OuterClass
    {
        #region Constructors

        public OuterClass()
        {
            Console.WriteLine("\nI am OUTER class constructor.\n");
            _position = "Software Engineer";
        }

        public OuterClass(string arg)
        {
            Console.WriteLine("\nI am OUTER class argumented constructor.\n");
            _position = "Software Engineer";
        }

        #endregion

        #region Class Members

        private static string _position = string.Empty;

        #endregion

        #region Public Methods

        public void DisplayPosition()
        {
            Console.WriteLine("\nOuter Class Position :" + _position);
        }

        #endregion

        #region Nested Classes

        public class InnerClass
        {
            #region Constructors

            public InnerClass()
            {
                Console.WriteLine("\nI am INNER class constructor\n");
                _position = "Computer Analyst.";
            }

            public InnerClass(string arg)
            {
                Console.WriteLine("\nI am INNER class argumented constructor\n");
            }

            #endregion

            #region Public Methods

            public void DisplayPosition()
            {
                Console.WriteLine("\nNested Class Position :" + _position);
            }
            #endregion
        }

        #endregion
    }


In above example we have Inner class inside the Outer class.

There are some constraints that applied in this type of structure.



1.       The top level class can only be marked as “Public” or “Internal”.

2.       The inner or nested classes can be marked with all modifies such as “Public”, “Private”, “Internal” and “Protected”.

3.       Like all other class members the default access modifier of inner or nested classes is “Private” that means the nested class or inner class cannot be accessed or referenced outside of the containing class.

4.       Important : nested or inner class has access to all (“Public”, “Private”, “Internal” and “Protected”) STATIC members of its container class. i.e. static Member variable, static methods etc.

5.       Important : since the nested classes are instantiated with reference to the enclosing class, their access protection level always remains less than or equal to that of the enclosing class. Hence, if the access level of the enclosing class is internal and the access level of the nested class is public, the nested class would only be accessible in the current assemble (project).



Enjoy!

No comments:

Post a Comment