Introduction

In my last post on the subject we explored the new “target typed new expressions” in C# 9. If you’re not read it you can read it here.

There were a number of mixed responses on Twitter and thought I’d explore them further.

Not a fan of ‘var’

This first response was quite interesting and I’d never thought about it before. When var was introduced I thought of it more as defining a variable with the type being specified on the right hand side of the equals. Howard brings up an interesting idea where if it had been called infer then it would have helped show the intent of the usage of the variable. This would have been an interesting usage. Some developers I’ve known really dislike the use of var all the time specially when the type is not implicit, such as a return from a method, but I’m not sure infer would have changed their minds. I do like the thought though of not just accepting the name but thinking about what it could have been. Thanks for the comment Howard.

Big fan, especially with Object Mothers

Kev is obviously a big fan of the new syntax. I’ve known Kev for a long time and he’s always been a fan of new syntax as it’s been introduced so this response was not really surprising. The interesting part of the reply for me was the suggested usage of the target typed new expressions of which to use them in unit tests with ObjectMothers.

So what is an Object Mother? It’s exactly what it sounds like, it is the mother of objects. To put it another way, it gives birth to instances aka “mothering them”. This is a concept mainly used during testing setup. Most tests will require data to run. When you manually create a moderately complex object graph there is the potential for a lot of duplication across tests. ObjectMothers can be thought of as “specific scenario factories” where common setup is kept in a single place and tweaks can be applied. These can either be used along with the builder pattern or you can ask the mother to give you a specific scenario.

Now that’s all a bit abstract so what does that mean in reality? Well I think that could be an entire post in itself so I will leave you with some further reading for now.

Field and property initializer fan

Now Frans made a very interesting point which I had not thought of during my investigation. I had concentrated on the consumption of an instance created by the usage of the new expressions but what about when you’re inside a class definition and you have some internal properties which need instantiating. Now I’ve had a bit of a play with this and I can see this being useful. I feel it will take a little getting used to it but could have potential.

public class MyPoco2
{
    private Dictionary<string, string> _fullDefinition = new Dictionary<string, string>();

    private Dictionary<string, string> _mini = new();
}

As you can see from the above _mini is much more concise and to the point compared to the declaration above which is doing exactly the same but more verbose.

Dan is also a fan and points out that Jetbrains Rider gives you some useful suggestions to simplify your code. If you’re a Resharper user in Visual Studio this also does the same as long as the project you’re in is targeting the right C# version. This is something I would like to investigate more in the future.

There is also agreement

James was on my page about usages. He also made a very interesting point about that even if you specify var you can still see the type if you hover over it with the tooltip and also “go to definition” if you’re on the var and it will take you to the type itself. Good productivity tip there. It also goes to show that it is only syntactic sugar.

The Total Fan

And to finish off we had Tom, who just loves them and thinks they are the future!

Conclusion

When I wrote the original post I didn’t think it would cause such a split and I thought the language level design idea Howard had was really insightful. I must admit in the past I’ve just taken language features on face value and only ask the questions “what are they?” and “how do I use them?” once they’re released. I’ve never had the mental bandwidth to splunk around the language design issues on GitHub to see how the sausage is made.

What are you thoughts? Do they fit with the above? Do you have something different to share? Please let me know on Twitter @WestDiscGolf!