C#中字符串插值内部的字符串插值会导致编译器错误

C#中字符串插值内部的字符串插值会导致编译器错误,c#,string-interpolation,csc,C#,String Interpolation,Csc,以下C#表达式导致我的程序中出现编译器错误: $"Getting image from {location.IsLatitudeLongitude ? $"{location.Latitude} - {location.Longitude}" : location.Location}." 难道不能像那样使用字符串插值吗?还是不可能做到这一点?我刚刚测试了这个。正如我所评论的,您需要为tenery操作符使用大括号: $"Getting image from {(location.IsLatitu

以下C#表达式导致我的程序中出现编译器错误:

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

难道不能像那样使用字符串插值吗?还是不可能做到这一点?

我刚刚测试了这个。正如我所评论的,您需要为tenery操作符使用大括号:

$"Getting image from {(location.IsLatitudeLongitude ? $"{location.Latitude} - {location.Longitude}" : location.Location)}."
根据,在字符串插值中使用三元运算符时,需要使用以下格式

插值字符串的结构如下所示:

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

在字符串插值中使用三元运算符有点棘手:我认为必须添加圆括号。
$"Getting image from {(location.IsLatitudeLongitude ? $"{location.Latitude} - {location.Longitude}" : location.Location)}."