Facilitating the spread of knowledge and innovation in professional software development

Contribute

### Topics

InfoQ Homepage News C# 8: Type Inference for the New Keyword

# C# 8: Type Inference for the New Keyword

This item in japanese

In many situations, there is only one possible type allowed in a given place. And yet C# still requires you to explicitly list the type. If the Target-typed new expression proposal is adopted, such boilerplate code will no longer be necessary.

Superficially, this feature looks like the reverse of the var keyword. But instead of inferring the variables type from an expression, it infers the expression’s type from a variable. Here is an example from the proposal using the current and proposed syntax.

Dictionary<string, List<int>> field = new Dictionary<string, List<int>>();
Dictionary<string, List<int>> field = new();


As you can see, it simply removes the need to specify the type when it can be inferred from the context. With local variables this isn’t too interesting, but where it can come in handy is short-lived arguments.

XmlReader.Create(reader, new XmlReaderSettings() { IgnoreWhitespace = true });


In the above code, the XmlReaderSettings class isn’t very interesting. It is merely a holder for options values to be passed to the XmlReader. One could even argue the type name XmlReaderSettings distracts from the important information.

Another place where this feature is useful is fields and properties where the var keyword is not an option. For example,

private readonly static object s_syncObj = new();

According to the proposal, there are four exceptions to its use.

• Enum types: not all enum types contain the constant zero, so it should be desirable to use the explicit enum member.
• Interface types: this is a niche feature and it should be preferable to explicitly mention the type.
• Array types: arrays need a special syntax to provide the length.
• Struct default constructor: this rules out all primitive types and most value types. If you wanted to use the default value of such types you could write default instead.

Status

Currently type inference for the new keyword is listed on the C# 8 roadmap with the status “Prototype”. The feature test plan is available on GitHub.

Style

## Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

• ##### Long overdue

by wakin imgen,

• ##### Re: Long overdue

by Cameron Purdy,

• ##### Re: Long overdue

by Jonathan Allen,

• ##### Re: Long overdue

by wakin imgen,

• ##### Re: Long overdue

by wakin imgen,

• ##### Re: Long overdue

by Jonathan Allen,

• ##### Copy+Paste error

by Mark Rendle,

• ##### Long overdue

by wakin imgen,

Your message is awaiting moderation. Thank you for participating in the discussion.

I forget how many times I think to myself when I declare Dictionary<TKey, TValue> in a class that this is so stupid that I have to type the stuff twice. Now finally it's being fixed. This kind of annoyance is really annoying.

• ##### Re: Long overdue

Your message is awaiting moderation. Thank you for participating in the discussion.

That you "have to type the stuff twice" is not something that I have experienced, but then again, I use IntelliJ.

However, this unbounded fascination with reducing typing as a proxy for actual language improvements is ultimately self-defeating. For example, efficiency improvements in typing code pale to efficiency improvements in the readability of code.

• ##### Re: Long overdue

Your message is awaiting moderation. Thank you for participating in the discussion.

In the XmlReader.Create example, the goal is to improve readability. The idea is to remove the boilerplate (XmlReaderSettings) in order to bring the important information (IgnoreWhitespace = true) to the forefront.

Whether or not it actually improves readability is a matter of opinion. I'm just relating the thought process behind it.

• ##### Re: Long overdue

by wakin imgen,

Your message is awaiting moderation. Thank you for participating in the discussion.

I don't think
Dictionary<int, int> _dict = new ();
Dictionary<int, int> _dict = new Dictionary<string, int>();

• ##### Re: Long overdue

by wakin imgen,

Your message is awaiting moderation. Thank you for participating in the discussion.

Somehow the formatting is hard to do in the comments. Why don't InfoQ adopt Markdown?

• ##### Re: Long overdue

Your message is awaiting moderation. Thank you for participating in the discussion.

When InfoQ was created, Markdown wasn't widely used for forum/comment systems. Changing it now would be difficult, but I'll bring it up with management as I do agree it would be nicer.

• ##### Copy+Paste error

by Mark Rendle,

Your message is awaiting moderation. Thank you for participating in the discussion.

The Status note at the bottom of the article references the Caller Expression Attribute, not the new() type inference feature.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p