Always initialized (not null) reference types (fields, props, parameters, returned values) #1127
Replies: 4 comments
-
To be clear, what you're asking for is going to be the default behavior of an un-annotated reference type when you opt-in to Nullable Reference Types. You can play around with this today. |
Beta Was this translation helpful? Give feedback.
-
I suggest that you read the C# Blog and use the GitHub search before opening new feature requests. |
Beta Was this translation helpful? Give feedback.
-
@MgSam Ok I will. I just didn't realize that I'm too late with my proposals. I stored in a txt-file all proposals, improvements and bugs I found over the years and now at last I have time to ask for them in github :) |
Beta Was this translation helpful? Give feedback.
-
This is very nice, I think warning is not enough. assigning this will also make programs less error prone and slightly faster. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Sometimes it is necessary that a parameter of reference type passed to a function always be initialized. For example see here sources of List:
https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/collections/generic/list.cs
Or I think it can be so:
It throws an exception every time when the parameter 'collection' is null.
I think it would be good to simplify this code with a feature that requires a type to be guaranteed initialized. Just add a symbol '!' at the end of a type. For example, this code:
should show an error at compile-time. But this code:
should be compiled successfully.
When a constructor
public MyClass(string! parameter)
is a part of API available for third-party applications and developers then it becomes even more important to have a possibility to mark a type as '!' because other developers that will use my API will see that a parameter type is "string!" and not "string", so they know that they can't pass null.
Another examples:
and
and
When it can't be checked at compile-time then it should throw a run-time exception. For example, ArgumentNullException(nameof(p)) in this function:
public Func1(string! p)
Beta Was this translation helpful? Give feedback.
All reactions