Answers for "c# when"

C#
0

c# when

// When keyword can be used in 
// - catch, case and switch expressions

// The syntax for when is as follows:

" catch (ExceptionType [e]) when (expr) "

// A real life example of when:

public static async Task<string> MakeRequest()
{
	// Try to make a request to a site
	try { return await client.GetStringAsync("https://localHost:10000"); }
	
	// You can separate your catch statements using the 'when' keyword
	// The when takes in a boolean parameter and runs when true
	catch (HttpRequestException e) when (e.Message.Contains("404"))
	{ return "Page Not Found"; }
	
	catch (HttpRequestException e) { return e.Message; }
}
Posted by: Guest on June-30-2021

Code answers related to "c# when"

C# Answers by Framework

Browse Popular Code Answers by Language