Search code examples
c#stringconditional-statementsstring-interpolation

C# String Interpolation and the Conditional Operator


How I can return null in the string if my {condition} is false. I'm using string interpolation with the conditional operator.

string returnURL = $"{ condition ? (ConfigurationManager.AppSettings["ApplicationURL"]}/my-page/{trip?.TripUId }) : null"


Solution

  • Use like this

    bool flag = true;
    string returnUrl = flag ? $"{ConfigurationManager.AppSettings["ApplicationURL"]}/my-page{trip?.TripUid}" : null;