Swtich does not support string constants? #63400
Answered
by
CyrusNajmabadi
zms9110750
asked this question in
Q&A
foreach (var ele in XElement.Load("D:\\temp.xml").Nodes())
{
switch (ele)
{
case XElement xel:
const string br = "br";
Console.WriteLine(xel.Name switch
{
"br" => "\n", //CS0150
br => "\n", //CS0150
S.br => "\n", //CS0150
});
break;
case XText txt:
Console.WriteLine(txt);
break;
}
}
static class S
{
public const string br = "br";
}Why does pattern matching not treat strings as constants |
Answered by
CyrusNajmabadi
Aug 15, 2022
Replies: 0 comments 1 reply
|
Problem here is that |
0 replies
Answer selected by
zms9110750
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem here is that
xel.Nameis an XName, which will never match any of those. use something likexel.Name.LocalName. THen all three are supported. I agree this message is confusing. @jaredpar to see if we can improve this here.