How to output the Ratio column when using BenchmarkDotNet

If you’re using BenchmarkDotNet and want to see how other methods compare against a baseline method, set the Baseline attribute on your baseline method. Doing this will result in the Ratio column being outputted as shown below… More info from the Benchmark and Job Baselines page on BenchmarkDotNet.org.

Omit type name when calling static methods with the using static directive in C#

The using static directive was introduced in C# 6 and allows us to omit the type name when calling static methods… cool right? … but I wouldn’t use this when omitting the type name would result in ambiguity or confusion for the next developer about what class a static method is in. The examples below … Continue reading Omit type name when calling static methods with the using static directive in C#

Using aliases in C# to provide shorthand references to types or namespaces

In C# we can use aliases to provide shorthand references to types or namespaces. Aliases can be useful to tidy up our code for example when we might be repeatability referencing generic types. They also help to remove ambiguity when we have conflicting type names in difference namespaces. In this case without aliases we would … Continue reading Using aliases in C# to provide shorthand references to types or namespaces

Declaring and setting multiple variables of the same type in the same statement in C#

Does anyone actually use this variation of declaring variables? I rarely see it. I think its possibly useful in instances where the variables are highly related (eg.. x, y or startDate, endDate). This way the reader can know they belong together but I wouldn’t be bunching a load of variables up just because they are … Continue reading Declaring and setting multiple variables of the same type in the same statement in C#

ToString() v nameof() performance comparison when getting a string value from a enum option

The benchmarks (originally published on LinkedIn) below from Guilherme Ferreira show’s that there’s a big performance difference between using ToString() and nameof() when we are trying to get the string representation of a particular enum value… Why? … Well we can see from the IL below that nameof is evaluated at compile time and basically hardcodes … Continue reading ToString() v nameof() performance comparison when getting a string value from a enum option

In C# use the nameof() expression so your property, class and namespace references are type safe

In C# if you’re referencing property, class or namespace names in your code use the nameof() expression rather than strings so your references are type safe… If you have string literals containing property, class and namespace names all around your code you have to maintain them manually. If you rename a class which is referenced … Continue reading In C# use the nameof() expression so your property, class and namespace references are type safe

StringBuilder performance can be improved in C# by setting its capacity

When using StringBuilder in .NET most of times we likely just use the default constructor StringBuilder()… BUT if we have a rough estimate of how long our final string will be setting the capacity to this estimate can improve performance of StringBuilder dramatically. StringBuilder performance in C# is improved when setting capacity because StringBuilder doesn’t … Continue reading StringBuilder performance can be improved in C# by setting its capacity

C#9 new AND, OR, NOT pattern matching keywords

C#9 (released Nov 2020) has some pattern matching enhancements. Added in C#9 (among other pattern changes) is conjunctive patterns (AND keyword), disjunctive patterns (OR keyword) and negative patterns (NOT keyword). Two examples are below. These are simple examples of course but basically you don’t have to repeat the variable name as before and the syntax … Continue reading C#9 new AND, OR, NOT pattern matching keywords