5

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?

4
  • 1
    it needs expression not statment. do pageTitle==""?"No title":PageTitle Commented Apr 6, 2023 at 20:48
  • 2
    Side note: code in the question does not have "the switch statement"...(medium.com/@time4ish/…) Commented Apr 6, 2023 at 20:53
  • The code blocks of interpolated strings must be expressions, they must evaluate to something, or "return" something, which is why the switch expression works because it returns something. Also tangential nitpick: use string.IsNullOrEmpty() which can then be used in a very clean ternary expression -> string header = $"Header: {(string.IsNullOrEmpty(pageTitle5) ? "No Title" : pageTitle5)}" Commented Apr 6, 2023 at 20:59
  • Ah! Thank you @AlexeiLevenkov. I was wondering why the syntax didn't match the docs for "switch statement". Thanks for the link. Commented Apr 6, 2023 at 21:13

2 Answers 2

6

Every expression will work. In C#, if is not an expression, but a statement.

However, the ternary operator yields an expression:

string header51 = $"Header: {
  (pageTitle5 == "" 
      ? "No title"
      : pageTitle5)
  }";

switch works in your example, because you do not use the switch statement but a switch expression.

Sign up to request clarification or add additional context in comments.

2 Comments

And anything goes if wrapped as IIFE
For anyone else reading this ... the above code doesn't work as is. The ternary operator expression needs to be within parens ( ) to compile.
1

If you prefer using if else statement, you can write the code like this :

    string header51 = $"Header:{() =>
    {
        if (pageTitle5 == "")
        {
            return "No title";
        }
        else
        {
            return pageTitle5;
        }  
    }
    }";

This way you have more flexibility to do extra logics in the if else block

2 Comments

Nice! So I can pass an anonymous function (or call a named function). Thanks!
@DeborahK No, this code doesn't work as you would expect. This is just defining the anonymous function, but there's nothing to execute it. It will just call .ToString() on it and use that. So with this code, you would get something like "Header:System.Func`1[System.String]" as the value assigned to header51. You can call a named function though, and since you're actually calling it, its result will be included in the string value. Maybe you could assign an anonymous function to a Func<T> and then execute it, but that seems overly convoluted for most uses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.