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

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

问题描述

<块引用>

可能重复:
C# - 有没有更好的替代开启类型"?

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

例子:

函数测试(object 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} 平方");休息;案例矩形 r:WriteLine($"{r.Length} x {r.Height} 矩形");休息;默认:WriteLine("<未知形状>");休息;案例空:抛出新的 ArgumentNullException(nameof(shape));}

在 C# 7 之前

没有.

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

<块引用>

我们收到了很多关于添加 C# 语言的请求,今天我将讨论其中一种更常见的——开关类型.开关类型看起来是一个非常有用且直接的功能:添加一个类似开关的构造,它打开表达,而不是价值.这可能看起来像这个:

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

<块引用>

这种语句对于添加 virtual 非常有用方法,例如在不相交的类型层次结构或类型上分派包含您不拥有的类型的层次结构.看到一个例子这样,您可以轻松得出结论,该功能将是简单实用.它甚至可能让你思考为什么不那些 #*&%$ 懒惰的 C# 语言设计者只是让我的生活更轻松,添加这个简单、省时的语言功能?"

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

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天全站免登陆