Click here to Skip to main content
15,918,706 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi here i am using switch case statement..But i dont want that switch case statement...so i want to use Dictionary here...but i dont know how to use dictionary here...so please any body help me how to use Dictionary method here....this is my switch case statement code...

C#
while (true)
                    {
                        Console.WriteLine("1.cash deposit");
                        Console.WriteLine("2.cash withdrawl");
                        Console.WriteLine("3.Balance enquiry");
                        Console.WriteLine("4.exit");
                        int option = int.Parse(Console.ReadLine());

                        switch (option)
                        {
                            case 1: account.Deposit(actn);
                                break;
                            case 2: account.withdraw(actn);
                                break;
                            case 3: account.enquiry(actn);
                                break;
                            case 4: Environment.Exit(0);
                                break;
                            default: Console.WriteLine("Please enter a valid options");
                                break;
                        }
                    }


thanks and regards
Posted
Updated 30-Dec-14 20:28pm
v2
Comments
Suvendu Shekhar Giri 31-Dec-14 1:57am    
I may be wrong but as far I know, Dictionary is not a replacement of Switch Case statement. While switch case is a type of conditional statement, Dictionary is used to represent collection of key-value pair. May be you want something else.
Shivaram_i 31-Dec-14 2:00am    
Hi shekhar thanks for your response....Not only dictionary...Is there any alternative way for switch case statement???Because switch case is static right? so i want another way...
Sergey Alexandrovich Kryukov 31-Dec-14 2:07am    
Of course, dictionary is the perfectly sensible idea. Please see my article referenced in my answer.
—SA
Sergey Alexandrovich Kryukov 31-Dec-14 2:05am    
Apparently, you did not get the idea at all; and the idea makes perfect sense. If you read my answer (actually, my article), you will probably understand it.
—SA
Shivaram_i 31-Dec-14 2:32am    
Hi sergey...may i know your article url...I didn't find it...

Please see my article on exactly this topic: Dynamic Method Dispatcher[^].

—SA
 
Share this answer
 
Comments
BillWoodruff 31-Dec-14 4:04am    
+5 as much for the Socratic dialogue you had with the OP via comments, as for your interesting article.
Sergey Alexandrovich Kryukov 31-Dec-14 13:32pm    
Thank you, Bill. Happy New Year!
—SA
I am impressed that the idea of having executable code as the Value of a KeyValuePair in a .NET Dictionary occurred to you. That's something I published an article about on DevX more than eight years ago: [^] ... do remember that article was written when some features we enjoy in C# .NET now were not available; I would not write the same article today :)

I think Sergey's article is a valuable resource for advanced study of this topic.

One thing to keep in mind, however: a switch/case statement is very efficient, particularly in the case where you use an integer value to determine which Case is executed, and that integer increases in regular order. In that case, the compiler can create a simple jump-table. Dictionaries in .NET are also speedy; under-the-hood they use hash-sets.

You can use the auto-initialize facility of the .NET dictionary, and Lambda expressions (newer addition to C# .NET) to simplify instantiation:
C#
Dictionary<int, Action<int>> BankTransactions = new Dictionary<int, Action<int>>
{

    {1, value => Deposit(value)},
    {2, value => WithDraw(value)},
    {3, value => Enquiry(value)},
    {4, value => Exit(0)}
};

// sample method
private void Deposit(int accountNumber)
{
    MessageBox.Show(string.Format("Deposit: {0}", accountNumber));
}

// test in some method
BankTransactions[1](1000);
However, consider the limitation you have created by using a Dictionary like this: you are "locked in" to the requirement that the executable code 'Value for each 'Key in the Dictionary is a method with one integer parameter that returns no result.

In contrast, the code you execute in a Switch/Case statement for each Case can be any code; you can call any method requiring any number of parameters and, if needed, use its return value.

To make the executable method/delegate associated with a Dictionary 'Key accept a variable number of parameters, or parameters of different types, would require late-binding through use of Dynamic, or some other technique, and would cancel any benefit you got from the Dictionary's high-performance look-up capability.

And, now ... okay, I didn't quite tell the "whole story;" look at this:
C#
private string someString = "a string";
private double someDouble = 199.994;

private void Deposit2(int accountNumber, string str, double dbl)
{
    MessageBox.Show(string.Format("Deposit: {0} string: {1} double: {2}", accountNumber, str, dbl));
}

// test in some method
BankTransactions.Add(5, value => Deposit2(value, someString, someDouble));
BankTransactions[5](actn);
I have just invoked a method that requires three parameters from a Dictionary Key/Value Pair where the Value Type is an Action with one parameter !

The fact I can do this does not "fill me with joy;" in fact, I recommend you never do this because it will make your code more difficult to read/maintain in the future. imho this "hidden feature" is not consistent with C#'s identify as a strongly-typed language.

I "love" using Dictionaries, but they are just another tool in your toolbox; use them wisely :)
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 31-Dec-14 13:33pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900