簡體   English   中英

字符串插值:C#編譯器中的錯誤?

[英]String interpolation: a bug in c# compiler?

string Test(bool @bool) => $"you're {@bool?"hired":"fired"} Have a nice day!";

上面的代碼導致編譯錯誤。 但為什么? 注意string test = $"this {"is"} working"; 作品。

冒號結束插值。 只需加上條件:

string Test(bool @bool) => $"you're {(@bool ? "hired":"fired")} Have a nice day!";

對於這個問題,您不能使用?,:之類的東西,要使用這些,您必須設置條件,確切地,您的條件應放在()例如:

string Test(bool @bool) => $"you're {(@bool ? "hired":"fired")} Have a nice day!";

您可以嘗試使用()包含您的?:運算符

string Test(bool @bool) => $"you're {(@bool ? "hired":"fired")} Have a nice day!";

$-字符串插值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM