可以在c#中切换case包含方法吗? [英] can switch case contain method in c#?

查看:202
本文介绍了可以在c#中切换case包含方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个计算器,我想简单地传递一个方法以防万一?但我仍然无法做到这一点?我的意思是我不想要执行任何类型的动作,以防这是为什么我有制作方法,但它不工作/

你可以指导我...感谢你的...

解决方案

更好的是你看到下面的链接。



http://www.homeandlearn.co.uk/csharp/csharp_s2p15.html [ ^ ]


您可以在Switch / Case子句中的Case块中执行任何操作在任何方法中做。当然,您无法在Switch / Case块中定义类,方法或结构,就像您无法在标准方法中定义它们一样(除非您使用反射或Lambda表达式做了一些非常深奥的东西,所以())但是,你为什么要这样做?



使用Switch / Case子句几乎是经典计算器示例中的标准.NET书籍给出:例如,如果我创建一组按钮,并将每个按钮的文本设置为一些基本计算,然后将相同的Click EventHandler分配给所有按钮:< pre lang =cs> // 假设您有一个TextBox,'tBoxResult,
// 您希望显示计算结果:
private void CalcBtnClick( object sender,EventArg e)
{
按钮buttonClicked =发送者 as 按钮;

// 确保我们有一个有效的按钮!
if (buttonClicked == null throw new ArgumentException( 传递给CalcBtnClick的参数不是Type'按钮);

// 此处的代码,用于获取当前要处理的操作数,
// 如果需要,可以将它们从字符串转换为double
// 仅用于测试:假设参数位于:
double arg1 = 10 0 ;
double arg2 = 20 0 ;

switch (buttonClicked.Text)
{
case +
// add
tBoxResult.Text =(arg1 + arg2).ToString();
break ;
case -
// subtract
tBoxResult.Text =(arg1 - arg2)。的ToString();
break ;
case *
// multiply
tBoxResult.Text =(arg1 * arg2)。的ToString();
break ;
}
}

使用包含按钮作为键的字典和Funcs(以Lambda表达式形式的方法),有一种更优雅的方法作为值,但它是.NET的一个非常先进的用途。 根据要求我将在此处演示。


一个swtich表达式或case标签必须是bool,char,string,integral,enum only

方法,任何其他类型都不能使用。



参考:

link [ ^ ]


i am making a calculator where i want simply pass a method in case? but still i am not able to do that? i mean i dont want perform any kind of action in case that's why i have make method but it not working/
can u plz guid me.. Thanks for in afvance...

解决方案

better u see that below link.

http://www.homeandlearn.co.uk/csharp/csharp_s2p15.html[^]


You can do anything in a Case block inside a Switch/Case clause that you can do in any method. Of course, you can't define a class, method, or struct in a Switch/Case block, just as you can't define those inside a standard method (unless you did some very esoteric stuff with reflection or Lambda Expressions, and so forth) ... but, why would you ever want to do that ?

Use of a Switch/Case clause is almost a standard in the classic Calculator example that many .NET books give: for example, if I create a set of Buttons, and set the text of each one of them to some basic calculation, and then assign the same Click EventHandler to all of them:

// assume you have a TextBox, 'tBoxResult, 
// where you want the result of the calculation displayed:
private void CalcBtnClick(object sender, EventArgs e)
{
    Button buttonClicked = sender as Button;

    // make sure we have a valid button !
    if(buttonClicked == null) throw new ArgumentException("argument passed to CalcBtnClick is not Type 'Button");

    // code here to get current operands to be processed,
    // if necessary convert them from strings to double
    // for testing only: assume arguments are in:
    double arg1 = 10.0;
    double arg2 = 20.0;

    switch (buttonClicked.Text)
    {
        case "+":
            // add
            tBoxResult.Text = (arg1 + arg2).ToString();
            break;
        case "-":
            // subtract
            tBoxResult.Text = (arg1 - arg2).ToString();
            break;
        case "*":
            // multiply
            tBoxResult.Text = (arg1 * arg2).ToString();
            break;
    }
}

There is a more "elegant" way to do this using a Dictionary holding Buttons as Keys, and Funcs (methods ... in the form of Lambda Expressions) as Values, but it is a very advanced use of .NET. On request I will demonstrate that here.


A swtich expression or case label must be a bool,char,string,integral,enum only
methods ,any other types cannot be used.

reference :
link [^]


这篇关于可以在c#中切换case包含方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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