Tag Archives: All

Visual Studio smart replace

Let’s say you need to replace an expression in many files in Visual Studio with another expression. The case in question is replacing

nameof(someVariable)
to
"someVariable"

Sure you can try to replace

nameof(
to
"

but this will leave the double quote open and we don’t want this. Searching on the internetz and finding some stackoverflow solution and with the help of a second pair of eyes from a colleague we managed to find a solution like so:

Hit CTRL-SHIFT-H in Visual Studio and check “Use Regular Expressions”. Then select your scope, for example “Entire solution” and use the following expressions:

Find what:
nameof\(([a-zA-z]+)\)

Replace with:
"$1"

This will do the job. Let’s analyze the expressions a bit. The “Find what:” one, first. ‘nameof’ is the literal occurence of ‘nameof’. The first round bracket is preceded by backslash because we need to escape it. This is because the round bracket is used in the regular expressions syntax while in our case we need to literally find it. This was the ‘\(‘ part.

Now there is the ‘([a-zA-z]+)’ part. This has several sub-parts. The round brackets mean that the whole segment should be captured. We’ll later see why and what this is. Next, the square bracket part means that this should match any letter be it uppercase or lowercase (yes, I didn’t handle digits or underscores etc. which may be used in parameter/variable names, that is left as an exercise to the reader :P). The plus sign means the square bracket part may occur multiple times. This means that a parameter/variable name may have more than one such character. Finally we close this part with the round bracket.

The final part in the “Find what:” expression is a literal closing round bracket which, again, is escaped by being prefixed with a backslash.

In the “Replace with:” expression, the captured part in the previous expression (what is enclosed in the regular-expression-syntax round brackets) is expressed as $1, as first numbered. Finally we enclose this in double quotes because that is what we need.

As for why I had to rollback from C# 6 nameof expression to a stringly-typed version, that’s a story for another time, I guess.

IEnumerable.All() gotcha

Today as I was inspecting why some condition was evaluated to true instead of false I found out a strange thing. The code is something like :

var someList = new List<Person>();
if (someList.All(v => v.Age > 18))
{
    Console.WriteLine("All are 18 or older.");
}
else
{
    Console.WriteLine("At least one is less than 18.");
}

I was expecting that in case of an empty collection the All method would return false. But it doesn’t. The “All are 18 or older.” string would be printed.

In my case one more simple condition solved this issue :

    if (someList.Any() && someList.All(v => v.Age > 18))

After this I read the manual (RTFM) and according to MSDN (in a community comment however) :

It’s important to note that Enumerable.All() returns true for empty sequences

So it’s a “doh” moment for me.

Watch out for this in your code.