In C# 11 we can now include newlines in an interpolated string. So we can write code like this:
string pageTitle = "";
string header = $"Header: {
pageTitle switch
{
"" => "No title",
_ => pageTitle
}}";
Is there a way to write other code here beyond the switch statement?
I tried an if
and it tells me that if
is an invalid expression term.
string header51 = $"Header: {
if (pageTitle5 == "")
{
"No title";
}
else
{
pageTitle5;
}
}";
Are there other statements beyond switch that work here?
pageTitle==""?"No title":PageTitle
– Brooderstring.IsNullOrEmpty()
which can then be used in a very clean ternary expression ->string header = $"Header: {(string.IsNullOrEmpty(pageTitle5) ? "No Title" : pageTitle5)}"
– Aa