September 10, 2024

Weird strings - 2 kinds

If you have been nosing around in C# code (as you should be), you may have noticed some string literals of a sort you have never seen before. Such as:
  @"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.

The one with the @

This one is the less interesting, so let's deal with it first. It allows you to do two things. You can run strings for as many lines as you like (which is not allowed in regulation string constants). Also you can stick backslashes in your strings verbatim, without taking special measures. This is for convenience of Windows system users who often write pathnames like this:
  @"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.

The one with the $

This one is much more useful, and something you may find yourself using almost every day. When you start a string with a $, you get permission to do what is called "variable interpolation". There is a fancy name for everything. The idea now is that you can stick the contents of variables into strings like so:
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"
Here is a complete program:
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

Questions of style

And for those who have endured to the bitter end, a bonus. I want to make some comments about the use of "var" and programming style. Some people get their underwear in a big knot over questions of programming style. If those people are your bosses, you have to go with their choices. In other cases you have to make your own decisions. There is a style of C# programming that has little or no hesitation about using "var" and letting the compiler work out types. This is fine as long as you have a firm understanding about what the compiler is going to do and are convinced that will work out well. I am of that school, at least for local variables, as in this example. For variables of larger scope, you ought to give a hard type and make your intent clear.

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.


Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org