Monday 11 January 2016

Switch case Example with recursive methods in C#

A simple example of switch case in c# with recursive method to take input and display result in console application.



using System;
class Program
{

    void ex()
    {
        Console.WriteLine("In which class you want to read in? I will tell you required fee for admission.");
        Console.WriteLine("a: std 1");
        Console.WriteLine("b: std 2");
        Console.WriteLine("c: std 3");
        Console.WriteLine("d: std 4");


        string choice =  Console.ReadLine();
        Console.WriteLine("____________________");
        Console.WriteLine("You have selected : " + choice);


        //if (choice == "a")
        //{
        //    Console.WriteLine("Fee of a is : " + 20 );
        //}
        //else if (choice == "b" || choice  == "c")
        //{
        //    Console.WriteLine("Fee of "+ choice.ToUpper() +" is : " + 40);
        //}

        switch (choice)
        {
            case "a":
                {
                    Console.WriteLine("Fee of " + choice.ToUpper() + " is : " + 20);
                }
                break;
            case "b":
            case "c":
                {
                    Console.WriteLine("Fee of " + choice.ToUpper() + " is : " + 40);
                }
                break;
            case "d":
                {
                    Console.WriteLine("Fee of " + choice.ToUpper() + " is : " + 60);
                }
                break;
            default:
                Console.WriteLine("Sorry !!! Invalid selection.");
                break;
        }

        Console.WriteLine("\n____________________");


        ex();
    }
    static void Main()
    {

        Program p = new Program();
        p.ex();
     

        Console.Read();
    }
 
}