question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Do not require type specification for constructors when the type is known

See original GitHub issue

Do not require type specification for constructors when the type is known.

Imagine this case:

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

becomes:

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

Obviously, constructor arguments and object initialization would be allowed.

This might be a simple optimization given the above example, but there are longer type specifications out there.

But when applied to this:

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

it would become this:

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

which is a lot more readable. In cases like this, the type is not really important, its properties are.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:15
  • Comments:24 (13 by maintainers)

github_iconTop GitHub Comments

10reactions
MadsTorgersencommented, Aug 15, 2016

This is attractive as a counterpart to deconstruction, and we are considering it for a future version of C#.

3reactions
neofujicommented, Aug 21, 2016

I think it is useful for array initialization.

public int[] indices = new[256];
Read more comments on GitHub >

github_iconTop Results From Across the Web

Target-typed new expressions - C# 9.0 draft feature ...
Do not require type specification for constructors when the type is known. Motivation. Allow field initialization without duplicating the type.
Read more >
Why can't I define a default constructor for a struct in .NET?
NET, a value type (C# struct ) can't have a constructor with no parameters. According to this post this is mandated by the...
Read more >
Constructors and member initializer lists
Constructors have no names and cannot be called directly. They are invoked when initialization takes place, and they are selected according to ...
Read more >
6.5 Writing Object Type Call Specifications
If the specification declares only attributes or call specifications, then the body is not required. If you implement all your methods in Java,...
Read more >
2.2. Creating and Initializing Objects: Constructors
Creating and Initializing Objects: Constructors¶. A Java class defines what objects of the class know (attributes) and what they can do (behaviors).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found