C# Nested classes or inner classes example

Program

using System;

namespace AboutClasses
{
    class NestedorInnerClasses
    {
        public enum maritalStatus {Unmarried=1,Married}
        public enum Gender { Male = 1, Female}
        class PersonalInfo
        {
            public string name;
            public string emailID;

            public class DependentDetails //Inner Class1
            {
                public string spouseName;
                public DateTime marriageDate;
                public class children       //Inner Class2
                {
                    public string childName;
                    public Gender Gender;
                }
            }
        }

        static void Main(string[] args)
        {
            PersonalInfo pInfo = new PersonalInfo();

            Console.WriteLine("Enter your Name");
            pInfo.name = Console.ReadLine();

            Console.WriteLine("Enter your email");
            pInfo.emailID = Console.ReadLine();

            Console.WriteLine("Marital Status (1.Unmarried / 2.Married)");
            maritalStatus mStatus =                                                              (maritalStatusint.Parse(Console.ReadLine());

            if(mStatus.Equals(maritalStatus.Married))
            {
                PersonalInfo.DependentDetails pInfoDep = new                                               PersonalInfo.DependentDetails();

                Console.WriteLine("Enter your spouse name");
                pInfoDep.spouseName = Console.ReadLine();

                Console.WriteLine("Enter your Marriage Date");
                pInfoDep.marriageDate =                                                               Convert.ToDateTime(Console.ReadLine());

                Console.WriteLine("How many kids you have?");
                int kidsCount =int.Parse(Console.ReadLine());

                PersonalInfo.DependentDetails.children[] pInfoDepCh = new                         PersonalInfo.DependentDetails.children[kidsCount];

                if (kidsCount!=0)
                {
                    for(int kid=0;kid<kidsCount;kid++)
                    {
                        pInfoDepCh[kid] = new                                                      PersonalInfo.DependentDetails.children();
                        
                       //Name of the child
                        Console.WriteLine("Enter your child {0}                                                                name",kid+1);
                        pInfoDepCh[kid].childName = Console.ReadLine();

                       //Gender of the child
                        Console.WriteLine("Enter the gender of child {0}                                             (1.Male / 2.Female)", kid + 1);
                        pInfoDepCh[kid].Gender =                                                              (Gender)int.Parse(Console.ReadLine());
                    }
                }

                Display(pInfo, pInfoDep, pInfoDepCh);
            }

            Console.ReadKey();

        }//main

        static void Display(PersonalInfo p,
                            PersonalInfo.DependentDetails pd,
                            PersonalInfo.DependentDetails.children[] pdc)
        {
            Console.WriteLine("Name {0}",p.name);
            Console.WriteLine("Email{0}",p.emailID);

            if(pd!=null)
            {
                Console.WriteLine("Spouse Name   {0}", pd.spouseName);
                Console.WriteLine("Marriage Date {0}", pd.marriageDate);

                if(pdc!=null)
                {
                    foreach(PersonalInfo.DependentDetails.children x in                                                                         pdc)
                    {
                        Console.WriteLine("ChildrenName{0}", x.childName);
                        Console.WriteLine("Gender      {0}", x.Gender);
                    }
                }
            }

        } //display

    }//class

}//namespace


Output

Enter your Name
Sultan
Enter your email
syedsultan@gmail.com

Marital Status (1.Unmarried / 2.Married)
2
Enter your spouse name
Mrs.sultan
Enter your Marriage Date
2010/5/10

How many kids you have?
2

Enter your child 1 name
Sultan-Daughter
Enter the gender of child 1 (1.Male / 2.Female)
2
Enter your child 2 name
Sultan-Son
Enter the gender of child 1 (1.Male / 2.Female)
1

Name Sultan
Email syedsultan@gmail.com
Spouse Name    Mrs.sultan
Marriage Date 5/10/2010 12:00:00 AM
ChildrenName Sultan-Daughter
Gender        Female
ChildrenName Sultan-Son
Gender        Male

No comments:

Post a Comment