0

I have this code where this inline variable { (hour > 12 ? "PM" : "AM") }

I create it and it works but I am confused because without the braces () I have some issue with code lots of red underlines there, infect compiler code suggestions refactor it with red underlines but after wrap my expression with () it works.

I just want to know what is status of () in this interpolation inline variable?

WriteLine($"{hour}:{minout}:{seconds} { (hour > 12 ? "PM" : "AM") }");
Liam
  • 27,717
  • 28
  • 128
  • 190
BabarQB
  • 65
  • 1
  • 1
    Just to make sure that you provide an example, but don't use this code really this way - `dateTime.ToString("HH:mm:ss tt")` is the right way to do this. P.S. `12:00` is 12 PM, not 12 AM. – Yeldar Kurmangaliyev Aug 15 '17 at 08:10
  • Thanks for your response. But i just create it for demo purpose. Just for learning some C# 6 and c# 7 features. Thanks i will use your suggestion in my projects. Thanks – BabarQB Aug 15 '17 at 08:12

2 Answers2

5

: operator is valid operator for string formatting which used in string interpolation

var date = new DateTime(2017, 8, 31);
var stringDate = $"Date is {date:yyyy.MM.dd}"; // "Date is 2017.08.31"

Without parenthesis : is considered as format string, but not conditional operator, so for using conditional operator you should wrap your conditional expression with parenthesis.

var stringDate = $"It is {(date.Hours > 12 ? "evening" : "morning")}";

Another approach (perhaps more readable) execute all expressions before formatting result

var dayPart = date.Hours > 12 ? "evening" : "morning";
var stringDate = $"It is {dayPart}";
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • I never understood why they allowed expression in there. I mean if you need it again later you need to copy paste your code. While using a computed variable (like in your second approach) makes it reusable at least plus it's much more readable. – Franck Aug 15 '17 at 10:59
1

The parentheses there denote an expression, whose evaluation is required first and then its outcome is that would be the input of the interpolated string.

For a formal approach please have a look at the following link

Expressions and search for the term Parenthesized expressions in this document.

Regarding the interpolated strings, you can find more info here. However the one thing that you have to keep in mind is it's structure:

$"<text> {<interpolated-expression> [,<field-width>] [<:format-string>] } <text> ..."

In your case the (...) is the interpolated-expression.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • Thanks for your response.But why code suggestion not refactor correct way. – BabarQB Aug 15 '17 at 08:14
  • @BabarQB You are welcome. To the extent that I am aware of about refactor suggestions, I could say that the compiler can't suggest you all the possible ways you can refactor your code. Certainly, it can spot many common patterns and then make you a suggestion. However, there are plenty of other patterns for which you will not get from any compiler / or any static analysis tool a suggestion for refactoring. – Christos Aug 15 '17 at 08:31