Beispiel #1
0
            //You are also allowed to create objects of outer class in inner class
            public void InnerClassObject()
            {
                Outer_Class oc = new Outer_Class();

                //Inner class can access both static and non-static members of oter class
                Console.WriteLine(Outer_Class.str);
            }
Beispiel #2
0
        // Main method
        static public void Main()
        {
            // Create the instance of outer class
            Outer_Class obj1 = new Outer_Class();

            obj1.OuterMethod();

            // This statement gives an error because
            // you are not allowed to access inner
            // class methods with outer class objects
            // obj1. InnerMethod();

            // Creating an instance of inner class
            Outer_Class.Inner_Class obj2 = new Outer_Class.Inner_Class();

            // Accessing the method of inner class
            obj2.InnerMethod();
            obj2.InnerClassObject();

            // Creating object of
            // the derived class
            Exclass obj = new Exclass();

            obj.func();
            obj.InnerClassObject();

            //accessing a static method inside a static inner class nested in a non-static outer class
            //Remember C# doesn't allow non-static methods inside a static class, whether inner or outer
            Outer_Class.StaticInnerClass.StaticInnerClassObject();
        }
            //You are also allowed to create objects of outer class in inner class
            public static void StaticInnerClassObject()
            {
                Outer_Class oc = new Outer_Class();

                //Inner class can access both static and non-static members of oter class
                Console.Write("StaticInnerClassObject: ");
                Console.Write(Outer_Class.str);
                Console.Write(", ");
                Console.WriteLine(oc.webAddress);
            }