我们如何在C#中使用字典方法 [英] HOW DO WE USE DICTIONARY METHOD IN C#

查看:80
本文介绍了我们如何在C#中使用字典方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在这里使用开关案例声明..但我不想要切换案例声明...所以我想在这里使用词典...但我不知道如何在这里使用词典...所以请任何身体帮帮我如何在这里使用Dictionary方法....这是我的switch case语句代码...



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...

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

推荐答案

请参阅我关于此主题的文章:动态方法调度程序 [ ^ ]。



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

—SA


令我印象深刻的是,你想到了将可执行代码作为.NET字典中KeyValuePair的值的想法。这是我八年多前在DevX上发表的一篇文章:[ ^ ] ...请记住,当我们在C#.NET中享受的一些功能现在不可用时,我们就写了这篇文章。我今天不会写同一篇文章:)



我认为谢尔盖的文章是进一步研究这个主题的宝贵资源。



但要记住一件事:switch / case语句非常有效,特别是在使用整数值来确定执行哪个Case,并且整数按正常顺序增加的情况下。在这种情况下,编译器可以创建一个简单的跳转表。 .NET中的字典也很快;他们使用哈希集。



您可以使用.NET字典的自动初始化工具和Lambda表达式(C#的新增功能)。 NET)简化实例化:
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:
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);

但是,请考虑限制你通过使用这样的字典创建:你被锁定到要求可执行代码'每个字的值'字典中的键是一个带有一个不返回结果的整数参数的方法。



相比之下,您在每个Case的Switch / Case语句中执行的代码可以是任何代码;你可以调用任何需要任意数量参数的方法,如果需要,可以使用它的返回值。



使可执行方法/委托与Dictionary'Key接受可变数量的参数或不同类型的参数需要通过使用Dynamic或其他技术进行后期绑定,并取消您从Dictionary的高性能查找功能中获得的任何好处。



而且,现在......好吧,我并没有完全讲述整个故事;看看这个:

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:

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);

我刚刚调用了一个需要来自Dictionary Key / Value Pair的三个参数的方法其中Value Type是一个带有一个参数的Action!



我能做到这一点的事实并没有让我满心欢喜;事实上,我建议你永远不要这样做,因为它会使你的代码在将来更难以阅读/维护。 imho这个隐藏的功能与C#的强类型语言不一致。



我喜欢使用词典,但他们只是另一个工具箱中的工具;明智地使用它们:)

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 :)


这篇关于我们如何在C#中使用字典方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆