C# interpolated string with conditional-operator [duplicate]
You need to put the string in parentheses within {}, so: {(1 == 1 ? “yes” : “no”)}.
You need to put the string in parentheses within {}, so: {(1 == 1 ? “yes” : “no”)}.
Whenever TIMTOWTDI (there is more than one way to do it), you should look for the pros and cons. Using “string interpolation” (the second) instead of “string concatenation” (the first): Pros: Is less typing Automatically calls to_s for you More idiomatic within the Ruby community Faster to accomplish during runtime Cons: Automatically calls to_s for … Read more
To complement marsze’s helpful answer: ${…} (enclosing the variable name in { and }) is indeed always necessary if a variable name contains special characters, such as spaces, ., or -. Not special are _ and – surprisingly and problematically – ?. Note: : is invariably interpreted as terminating a PowerShell drive reference, in the … Read more
According to The Ruby Programming Language by Flanagan and Matsumoto: When the expression to be interpolated into the string literal is simply a reference to a global, instance or class variable, then the curly braces may be omitted. So the following should all work: @var = “Hi” puts “#@var there!” #=> “Hi there!” @@var = … Read more
I had this same question, and after a lot of research, it looks like it’s not possible. The answer from cgat is on the right track, but you can’t actually concatenate references like that. Here are things you can do with “variables” in YAML (which are officially called “node anchors” when you set them and … Read more
You can specify a format string after an expression with a colon (:): var aNumberAsString = $”{aDoubleValue:0.####}”;
Escaping with a backslash(\) works for all characters except a curly brace. If you are trying to escape a curly brace ({ or }), you must use {{ or }} per $ – string interpolation (C# reference) … All occurrences of double curly braces (“{{“ and “}}”) are converted to a single curly brace.
This is a change that was made in this pull request due to the fact that interpolating Optional(…) into the resultant string is often undesirable, and can be especially surprising in cases with implicitly unwrapped optionals. You can see the full discussion of this change on the mailing list here. As mentioned in the pull … Read more
This has been added as of C# 6.0 (Visual Studio 2015+). Example: var planetName = “Bob”; var myName = “Ford”; var formattedStr = $”Hello planet {planetName}, my name is {myName}!”; // formattedStr should be “Hello planet Bob, my name is Ford!” This is syntactic sugar for: var formattedStr = String.Format(“Hello planet {0}, my name is … Read more
You can combine $ and @ together to get a multiline interpolated string literal: string s = $@”Height: {height} Width: {width} Background: {background}”; Source: Long string interpolation lines in C#6 (Thanks to @Ric for finding the thread!)