The Daily WTF
C# has always had some variation on “string interpolation”, although starting in C# version 6, they added an operator for it, to make it easier. Now, you can do something like $”{foo} has a {bar}”, which can be a very easy to read method of constructing formatted strings. In this example, {foo} and {bar} will be replaced by the value of variables with the same name.
C#’s implementation is powerful. Pretty much any valid C# expression can be placed inside of those {}. Unfortunately for Petr, that includes the string interpolation operator, as a co-worker’s code demonstrates…
string query = $@”SELECT someColumn1, someColumn2, someColumn3 FROM myTable WHERE (TimeStamp >= ‘{startTime}’ AND TimeStamp < ‘{endTime}’) {(filterByType ? $”AND (DataType IN ({filetrOptions.Type}))” : string.Empty)}”; // Continued in this style for few more rows
This interpolated string contains an interpolated string. And a ternary. And it’s for constructing a SQL query
To read the full article click on the 'post' link at the top.