将 string.Contains() 与 switch() 一起使用
我正在做一个 C# 应用程序,我使用的地方
if ((message.Contains("test")))
{
Console.WriteLine("yes");
} else if ((message.Contains("test2"))) {
Console.WriteLine("yes for test2");
}
有什么方法可以更改为 switch()
的 if()
语句?
I'm doing an C# app where I use
if ((message.Contains("test")))
{
Console.WriteLine("yes");
} else if ((message.Contains("test2"))) {
Console.WriteLine("yes for test2");
}
There would be any way to change to switch()
the if()
statements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
正确的最终语法 [Mr. C]的回答。
随着 VS2017RC 的发布及其 C#7 支持,它的工作方式如下:
您应该注意案例排序,因为将选择第一个匹配项。这就是为什么“test2”被放置在测试之前。
Correct final syntax for [Mr. C]s answer.
With the release of VS2017RC and its C#7 support it works this way:
You should take care of the case ordering as the first match will be picked. That's why "test2" is placed prior to test.
这将在 C# 8 中使用 switch 表达式进行工作。
更多参考
https://learn.microsoft.com/en -us/dotnet/csharp/语言参考/运算符/开关表达式
This will work in C# 8 using a switch expresion
For further references
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression
不,switch 语句需要编译时常量。语句
message.Contains("test")
可以根据消息评估 true 或 false,因此它不是常量,因此不能用作 switch 语句的“case”。Nope, switch statement requires compile time constants. The statement
message.Contains("test")
can evaluate true or false depending on the message so it is not a constant thus cannot be used as a 'case' for switch statement.如果你只想使用 switch/case ,你可以这样做,伪代码:
但如果键的数量很大,你可以用字典替换它,如下所示:
If you just want to use
switch/case
, you can do something like this, pseudo-code:But if the quantity of keys is a big, you can just replace it with dictionary, like this:
使用 C# 简单而高效
Simple yet efficient with c#
您可以先进行检查,然后根据需要使用开关。
例如:
那么
You can do the check at first and then use the switch as you like.
For example:
Then
在确定环境时面对这个问题,我想出了以下一句话:
这样,如果它在提供的字符串中找不到与“switch”条件匹配的任何内容,它就会放弃并返回
null
。这可以很容易地修改以返回不同的值。它严格来说不是一个 switch,更多的是一个级联的 if 语句,但它很简洁并且有效。
Faced with this issue when determining an environment, I came up with the following one-liner:
That way, if it can't find anything in the provided string that matches the "switch" conditions, it gives up and returns
null
. This could easily be amended to return a different value.It's not strictly a switch, more a cascading if statement but it's neat and it worked.
一些自定义开关可以这样创建。也允许执行多个案例,
像这样调用
Some custom swtich can be created like this. Allows multiple case execution as well
Call like this
Stegmenn 为我找到了它,但是当您使用 IEnumerable 而不是像他的示例中那样的
string = message
时,我做了一个更改。Stegmenn nalied it for me, but I had one change for when you have an IEnumerable instead of a
string = message
like in his example.这将在 C# 7 中运行。在撰写本文时,它尚未发布。但是如果我理解正确,这段代码将会工作。
来源:https:// /blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/
This will work in C# 7. As of this writing, it has yet to be released. But if I understand this correctly, this code will work.
Source: https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/