Sponsored Ad

Saturday, May 5, 2012

How Nested Switch Statements Works in C#

Sometimes you need to implement the nested switch statements (Switch statement inside another switch) in C#. The program shown below will help you to understand the logic and implementation of nested switches.

The first switch decide that person is male or female and then inner switch decide that person is married or not.

image

using System;

namespace ConsoleLab
{
    class Programs
    {
        static void Main(string[] args)
        {
            string strSex = "Male";
            string strMaritalStatus = "UnMarried";

            switch (strSex)
            {
                case "Male":
                    switch (strMaritalStatus)
                    {
                        case "Married":
                            Console.WriteLine("The Person is Male and Married");
                            break;
                        case "UnMarried":
                            Console.WriteLine("The Person is Male and UnMarried");
                            break;
                    }
                    break;
                case "Female":
                    switch (strMaritalStatus)
                    {
                        case "Married":
                            Console.WriteLine("The Person is Male and Married");
                            break;
                        case "UnMarried":
                            Console.WriteLine("The Person is Male and UnMarried");
                            break;
                    }
                    break;
                default:
                    Console.WriteLine("Niether Male nor Female");
                    break;
            }
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment

Development Updates