The new String Interpolation and Ternary Operator

A quick tip for the new C# string interpolation. The new string interpolation has made things more simpler and readable. One might be tempted to include ternary operators within string interpolation.

However if you attempt something like following, the compiler would throw an error

Console.WriteLine($"{1==1?"Msg1":"Msg2"}");

The fix is as simple as it can get. Just include a brace within your ternary statement.

Console.WriteLine($"{(1==1?"Msg1":"Msg2")}");

That’s it. Brace up for a week with lot of fun and coding.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s