c# 6.0 - Confuse about String Interpolation in C# 6.0 -


i have code inline variable { (hour > 12 ? "pm" : "am") }

i create , works confused because without braces () have issue code lots of red underlines there, infect compiler code suggestions refactor red underlines after wrap expression () works.

i want know status of () in interpolation inline variable?

writeline($"{hour}:{minout}:{seconds} { (hour > 12 ? "pm" : "am") }"); 

: operator valid operator string formatting used in string interpolation

var date = new datetime(2017, 8, 31); var stringdate = $"date {date:yyyy.mm.dd}"; // "date 2017.08.31" 

without parenthesis : considered format string, not conditional operator, using conditional operator should wrap conditional expression parenthesis.

var stringdate = $"it {(date.hours > 12 ? "evening" : "morning")}"; 

another approach (perhaps more readable) execute expressions before formatting result

var daypart = date.hours > 12 ? "evening" : "morning"; var stringdate = $"it {daypart}"; 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -