@"In this world," $"Of toil and sin,"The weird part is the "prefix" character of @ or $. In the cases above, these do absolutely nothing, yet are harmless and could have been omitted.
@"C:\Users\wally\burma.txt"Without the @ this would need to be:
"C:\\Users\\wally\\burma.txt"Another case, that I find much more useful is when you are writing regular expressions, which tend to use a lot of backslash characters. Such as these examples:
string pattern = @"\b[M]\w+"; string pattern2 = @"^((\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)\s*[;]{0,1}\s*)+$";Regular expressions like these are bad enough without having to deal with a pair of backslashes every place where the above have just one.
myname = "Seymour"; Console.WriteLine ( $"My names is {myname}" );You can do this for int variables, float variables, and most anything. If you want to be fussy (as you might in the case of float variables) you can append a "format specifier" (that might actually be the proper fancy name) as in the following:
var value = 1.0 / 3; Console.WriteLine ( $"The value is {value:f2}" );This yields: "The value is 0.33"
using System; namespace HogHeaven { public class Program { public static void Main(string[] args) { var value = 1.0 / 3; Console.WriteLine ( $"The value is {value:f2}" ); } } } // THE END
I have my own style preferences about where to put the {} that begin and end blocks. I also like to make abundant use of whitespace, as you can see in the above. This makes some people crazy -- but if I am providing the sweat and writing the code, they just have to deal with it -- especially if they aren't paying my salary.
I also like to write methods like this in my "production code":
public static async Task new_wait ( string msg ) { ... }I put all those declarative words on one line, and the name of the method starts the next line. This lets me use my editor to search for method definitions using a pattern like: "^new_wait" that anchors the search to the left side. This is no big deal in small examples, but in a 1000+ line file with lots of references to the method in question, it can be a nice help.
Tom's Computer Info / tom@mmto.org