Use string.Contains() with switch()

前端 未结 12 2113
抹茶落季
抹茶落季 2020-12-25 10:39

I\'m doing an C# app where I use

if ((message.Contains(\"test\")))
{
   Console.WriteLine(\"yes\");
} else if ((message.Contains(\"test2\"))) {
   Console.W         


        
12条回答
  •  死守一世寂寞
    2020-12-25 11:03

    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.

    switch(message)
    {
        case Contains("test"):
            Console.WriteLine("yes");
            break;
        case Contains("test2"):
            Console.WriteLine("yes for test2");
            break;
        default:
            Console.WriteLine("No matches found!");
    }
    

    Source: https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

提交回复
热议问题