Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Interpolation inside String Interpolation in C# results in compiler error [duplicate]

The following C# expression is resulting in a compiler error in my program:

$"Getting image from {location.IsLatitudeLongitude ? $"{location.Latitude} - {location.Longitude}" : location.Location}."

Shouldn't it be possible to use String Interpolation like that? Or is it just not possible to do this?

like image 287
Thomas Gassmann Avatar asked Jun 19 '26 12:06

Thomas Gassmann


1 Answers

As per the documentation, you need to use the following format when using the ternary operator inside string interpolation.

The structure of an interpolated string is as follows:

$ "{ <interpolation-expression> <optional-comma-field-width> <optional-colon-format> }"

Therefore you need to add a set of brackets after { and before the closing } like this:

$"Getting image from {(location.IsLatitudeLongitude ? $"{location.Latitude} - {location.Longitude}" : location.Location)}."
like image 128
Kevin Holditch Avatar answered Jun 23 '26 18:06

Kevin Holditch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!