在 c# 类型上切换大小写 [英] Switch case on type c#

查看:29
本文介绍了在 c# 类型上切换大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
C# - 有没有更好的除了打开类型"之外,还有其他选择吗?

你好,假设我在类类型上有一个很大的 if/else.有没有办法用开关盒来做到这一点?

示例:

函数测试(对象obj){if(obj 是 WebControl){}else if(obj是TextBox){}else if(obj 是 ComboBox){}

等等……

我想创建类似

switch(obj){案例是文本框:休息;案例是组合框:休息;}

}

解决方案

更新 C# 7

是:来源

开关(形状){案例圈c:WriteLine($"半径为{c.Radius}的圆");休息;case Rectangle s when (s.Length == s.Height):WriteLine($"{s.Length} x {s.Height} 平方");休息;case 矩形 r:WriteLine($"{r.Length} x {r.Height} 矩形");休息;默认:WriteLine("<未知形状>");休息;情况为空:throw new ArgumentNullException(nameof(shape));}

在 C# 7 之前

没有.

http://blogs.msdn.com/b/peterhal/archive/2005/07/05/435760.aspx

<块引用>

我们收到了很多对 C# 语言的添加请求,今天我将谈论一种更常见的 - 打开类型.打开类型看起来是一个非常有用和简单的功能:添加一个类似 switch 的构造,它可以打开表达,而不是价值.这可能看起来像这个:

switch typeof(e) {case int: ... 中断;案例字符串:...中断;case double: ... 打破;默认值:...中断;}

<块引用>

这种语句对于添加虚拟方法,例如通过不相交的类型层次结构或类型进行调度包含您不拥有的类型的层次结构.看到一个例子这样,您可以很容易地得出结论,该功能将是简单实用.它甚至可能让你思考为什么不那些#*&%$ 懒惰的 C# 语言设计师只会让我的生活更轻松添加这个简单、省时的语言功能?"

不幸的是,像许多简单"的语言特性一样,类型开关是并不像最初看起来那么简单.当你看的时候,麻烦就开始了一个更重要但同样重要的例子如下:

class C {}接口我{}D 类:C,我 {}切换类型(e){案例C:……打破;情况一:……休息;默认值:……中断;}

链接:https:///blogs.msdn.microsoft.com/peterhal/2005/07/05/many-questions-switch-on-type/

Possible Duplicate:
C# - Is there a better alternative than this to 'switch on type'?

Hello suppose i get a big if/else on class type. it's there a way to do it with a switch case ?

Example :

function test(object obj)
{
if(obj is WebControl)
{

}else if(obj is TextBox)
{

}
else if(obj is ComboBox)
{

}

etc ...

I would like to create something like

switch(obj)
{
case is TextBox:
break;
case is ComboBox:
break;

}

}

解决方案

Update C# 7

Yes: Source

switch(shape)
{
    case Circle c:
        WriteLine($"circle with radius {c.Radius}");
        break;
    case Rectangle s when (s.Length == s.Height):
        WriteLine($"{s.Length} x {s.Height} square");
        break;
    case Rectangle r:
        WriteLine($"{r.Length} x {r.Height} rectangle");
        break;
    default:
        WriteLine("<unknown shape>");
        break;
    case null:
        throw new ArgumentNullException(nameof(shape));
}

Prior to C# 7

No.

http://blogs.msdn.com/b/peterhal/archive/2005/07/05/435760.aspx

We get a lot of requests for addditions to the C# language and today I'm going to talk about one of the more common ones - switch on type. Switch on type looks like a pretty useful and straightforward feature: Add a switch-like construct which switches on the type of the expression, rather than the value. This might look something like this:

switch typeof(e) { 
        case int:    ... break; 
        case string: ... break; 
        case double: ... break; 
        default:     ... break; 
}

This kind of statement would be extremely useful for adding virtual method like dispatch over a disjoint type hierarchy, or over a type hierarchy containing types that you don't own. Seeing an example like this, you could easily conclude that the feature would be straightforward and useful. It might even get you thinking "Why don't those #*&%$ lazy C# language designers just make my life easier and add this simple, timesaving language feature?"

Unfortunately, like many 'simple' language features, type switch is not as simple as it first appears. The troubles start when you look at a more significant, and no less important, example like this:

class C {}
interface I {}
class D : C, I {}

switch typeof(e) {
case C: … break;
case I: … break;
default: … break;
}

Link: https://blogs.msdn.microsoft.com/peterhal/2005/07/05/many-questions-switch-on-type/

这篇关于在 c# 类型上切换大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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