英文:
How to use indexers in switch expression?
问题
如何在switch表达式中访问索引器?在switch表达式中有一个很好的属性模式语法,但我无法弄清楚或找到关于如何使用索引器的任何信息。
给定以下代码:
var a = "123";
if(a.Length == 3 && a[0] == '1')
Console.WriteLine("passed");
如何将其转换为switch表达式?匹配 a.Length
很容易,但如何指定匹配 a[0] == '1'
?
var b = a switch
{
{ Length: 3, [0]: '1' } => "passed", // CS8918: 预期标识符或简单成员访问。
_ => "error"
};
Console.WriteLine(b);
英文:
How to access indexers in switch expression? There is a nice property pattern syntax in switch expressions, but I can't figure out or find any information about using indexers.
Given following code:
var a = "123";
if(a.Length == 3 && a[0] == '1')
Console.WriteLine("passed");
How do to convert it to switch expression? Matching a.Length
is easy, but how to specify a match for a[0] == '1'
?
var b = a switch
{
{ Length: 3, this[0]: '1' } => "passed", // CS8918: Identifier or a simple member access expected.
_ => "error"
};
Console.WriteLine(b);
答案1
得分: 4
一种方法是使用when
语句。这不像属性模式那么漂亮,但可以作为一种解决方法:
var b = a switch
{
{ Length: 3 } when a[0] == '1' => "passed",
_ => "error"
};
英文:
One way to do it is by using when
statement. It is not as beautiful as property pattern, but can be a workaround:
var b = a switch
{
{ Length: 3 } when a[0] == '1' => "passed",
_ => "error"
};
答案2
得分: 4
自 C# 11 开始,您可以使用 list patterns:
var b = a switch
{
['1', _, _] => "passed", // CS8918: 预期标识符或简单成员访问。
_ => "error"
};
英文:
Since C# 11 you can use list patterns:
var b = a switch
{
['1', _, _] => "passed", // CS8918: Identifier or a simple member access expected.
_ => "error"
};
答案3
得分: 1
自 C# 8.0
版本开始,您可以使用 switch 表达式来实现它:
var a = "123";
var tuple = (a.Length, a[0]);
switch (tuple)
{
case (3, '1'):
//执行某些操作
break;
case (4, '2'):
//执行某些操作
break;
}
或者
string b = (a.Length, a[0]) switch
{
(3, '1') => "通过",
(4, '2') => "失败",
_ => "错误"
};
您可以在此文档中找到更多关于 switch 表达式的用法(https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression)
编辑:
在需要在评估 a[index] 之前检查 a.Length 的情况下,您实际上可以在创建新元组时进行检查。
var a = "323";
var index = 5;
char? ch = (a.Length >= index + 1) ? (char?)a[index] : null;
var b = (a.Length, ch) switch
{
(_, null) => "错误",
(3, '1') => "通过",
_ => "默认"
};
System.Console.WriteLine(b ?? "");
英文:
Since c# 8.0
. You can acheive it with switch expressions:
var a = "123";
var tuple = (a.Length, a[0]);
switch (tuple)
{
case (3, '1'):
//do something
break;
case (4, '2'):
//do something
break;
}
or
string b = (a.Length, a[0]) switch
{
(3, '1') => "passed",
(4, '2') => "failed",
_ => "error"
};
You can find more usage of switch expressions from this documentation (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression)
Edit:
In a case where you need to check a.Length before evaluating a[index].
You can actually check it when creating a new Tuple.
var a = "323";
var index = 5;
char? ch = (a.Length >= index + 1) ? (char?)a[index] : null;
var b = (a.Length, ch) switch
{
(_, null) => "error",
(3, '1') => "pass",
_ => "default"
};
System.Console.WriteLine(b??"");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论