Support non-nullable value types with the InitRequiredForNotNullAttribute
See original GitHub issueThe InitRequiredForNotNullAttribute
is really great, we’ve activated it in all our projects but there is one missing piece in my opinion: the non-nullable value types!
Consider this code:
using System;
class Program
{
static void Main()
{
_ = new Person
{
Name = "Foo"
};
}
}
class Person
{
public string Name { get; set; }
public DateTime BirthDate { get; set; }
}
I’m expecting to get the CSE001 error but actually not because BirthDate
is a value type.
Would that be possible to implement the same behavior as for non-nullable reference types? That would be awesome.
Thank you
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
c# - Non-nullable reference types' default values VS ...
A Value property of type T An instance for which HasValue is true is said to be non-null. A non-null instance contains a...
Read more >Configure non-nullable types as required · Issue #2036
In my project we do not distinguish between required and nullable: false. If a type is non-nullable, it is also required.
Read more >Nullable reference types
This article provides an overview of nullable reference types. You'll learn how the feature provides safety against null reference ...
Read more >C# 8.0 nullable references: NotNull
The NotNull attribute enables C#8's nullable references feature to provide more useful warnings, by helping it infer information about a ...
Read more >Digging Into Nullable Reference Types in C#
Classes and Nullable Reference Types Properties that aren't nullable are expected to be set before the end of the constructor. There are two ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@cezarypiatek Tested this change and it’s working, but I found something I wouldn’t expect.
Given the following code:
I get
CSE001 - Missing initialization for properties: - IsAdmin
, which I wouldn’t expect as a default value is being set. At the very least an[InitOptional]
attribute could exist to allow disabling the initialization checks for a single property.– Edit: Thinking about this again, it’s probably only an issue when using the attribute globally in the assembly/type, as otherwise you just wouldn’t specify the attribute for the property. Anyway, I think it still makes sense to have a
[InitOptional]
for such cases.@jmevel @vsilvar thanks for the feedback. I’ve just added
[InitOptional]
which allows for excluding a specific member or whole type from the mandatory init imposed on the higher level by [InitRequired] or [InitRequiredForNotNullAttribute]. Should be available in v4.1Please let me know how do you find the current behavior. I’m not sure if
CSE004: InitOnlyOptional requires default value
should be reported for fields marked with[InitOptional
as well. I haven’t implemented it but I can do it. What do you think?