-2

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"

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
awais
  • 492
  • 4
  • 17
  • add parenthesis: `$"{(condition ? ... : ...)}"` – Dmitry Bychenko Jun 16 '20 at 13:06
  • 2
    It's unclear to me what you're trying to achieve. Do you want `returnURL` itself to be null if `condition` is false? A [mcve] with sample input and output would make it much easier to help you. – Jon Skeet Jun 16 '20 at 13:11
  • Send the error message. – Amin Golmahalleh Jun 16 '20 at 13:11
  • 3
    (I suspect you should just use the conditional operator *outside* the interpolated string literal: `string returnUrl = condition ? $"{ConfigurationManager.AppSettings["ApplicationURL"]}/my-page{trip?.TripUid}" : null;` – Jon Skeet Jun 16 '20 at 13:12

1 Answers1

1

Use like this

bool flag = true;
string returnUrl = flag ? $"{ConfigurationManager.AppSettings["ApplicationURL"]}/my-page{trip?.TripUid}" : null;
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197