Sponsored Ad

Sunday, May 6, 2012

Example to Use Class Inside Another Class in C#

You can have a class inside another class (nested class). The example of nested class is given below. The program also tells that how to use each class variable and create object of each class.

The full syntax of nested class is written below.

Incase of any questions, comment below.

Example to Use Class Inside Another Class in C#

using System;

namespace ConsoleHub
{
    class Programs
    {
        string strPrograms = "Prgram class string.";
        class SubClass
        {
            string strSubClass = "SubClass class string.";

            static void Main(string[] args)
            {
                ConsoleHub.Programs objPrograms = new ConsoleHub.Programs();
                ConsoleHub.Programs.SubClass objSubClass = new ConsoleHub.Programs.SubClass();

                Console.WriteLine("The value of Programs Class Variable is: {0}", objPrograms.strPrograms);
                Console.WriteLine("The value of SubClass Class Variable is: {0}", objSubClass.strSubClass);

                Console.ReadLine();

            }
        }
    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates