Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

c# switch expression pattern matching

string startsWith = "somestring:";

 switch (startsWith)
 {
	 // Using the 'when' keyword you can convert your case to a bool like
     // expression like so:
     case string when startsWith.StartsWith("somestring:"):
         Console.WriteLine("hit");
         break;

   case string when startsWith.StartsWith("someotherstring:"):
		Console.WriteLine("hit 1");
		break; 
}

// Output: hit
 
PREVIOUS NEXT
Tagged: #switch #expression #pattern #matching
ADD COMMENT
Topic
Name
4+7 =