Replies: 5 comments 1 reply
-
Just so you know, it could create issues around passing the class to generic code with new() constraint, where class is constructed with new T(): T create<T>() where T : new() => new T();
//test.Name is null anyway here
var test = create<Test>(); I think this is an issue, but could be resolved with further language work (for example enabling call |
Beta Was this translation helpful? Give feedback.
-
Yeah, the current situation is suboptimal when using NRTs. |
Beta Was this translation helpful? Give feedback.
-
You're not the only one. Also see the arguments made by myself and @HaloFour in #2328 |
Beta Was this translation helpful? Give feedback.
-
One could argue that these types should not have a default constructor, since the default constructor would put the object in an invalid state.
and the compiler could automatically add that dummy argument to any explicit call to |
Beta Was this translation helpful? Give feedback.
-
@erik-kallen The code that such classes appear the most for me are simple data objects, where usually you have multiple data properties (for example 20+) which you have to copy from somewhere else. Writing constructors with that many arguments is annoying and error prone. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Just tried preview of new C#, and I've noticed one thing that could be improved.
When I declare class with
init
non-nullable properties:I get a warning:
Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
What I thought would happen, is that I would only get nullable warning in the place where I try to construct the object without setting properties:
This would effectively reduce the need to use nullable annotations on properties that are never really intended to be null, but you have to annotate them anyway in order to satisfy the analyzer.
Any thoughts?
Beta Was this translation helpful? Give feedback.
All reactions