-
Mar 21st, 2024, 03:57 PM
#1
[RESOLVED] Everything is Nullable???
It seems like C# wants you to explicitly state that a type is nullable even if it is a reference type. For example, I'm getting a warning on this line:
Code:
PropertyInfo property = type.GetProperty(attributeName);
That could easily return null. In fact, under many circumstances, it SHOULD. That means that the "property" variable can be null. The very next line in the code checks to see whether it is null.
The warning states "Converting null literal or possible null value to non-nullable type." However, PropertyInfo is a class, and all classes are nullable.
The class is here:
https://learn.microsoft.com/en-us/do...o?view=net-8.0
Changing the offending line to
Code:
PropertyInfo? property = type.GetProperty(attributeName);
does make the warning go away, but this should not be necessary.
My usual boring signature: Nothing
 
-
Mar 21st, 2024, 04:00 PM
#2
Re: Everything is Nullable???
 Originally Posted by Shaggy Hiker
It seems like C# wants you to explicitly state that a type is nullable even if it is a reference type. For example, I'm getting a warning on this line:
Code:
PropertyInfo property = type.GetProperty(attributeName);
That could easily return null. In fact, under many circumstances, it SHOULD. That means that the "property" variable can be null. The very next line in the code checks to see whether it is null.
The warning states "Converting null literal or possible null value to non-nullable type." However, PropertyInfo is a class, and all classes are nullable.
The class is here:
https://learn.microsoft.com/en-us/do...o?view=net-8.0
Changing the offending line to
Code:
PropertyInfo? property = type.GetProperty(attributeName);
does make the warning go away, but this should not be necessary.
https://learn.microsoft.com/en-us/do...ble-references gives a bit more background on nullable reference types and some of the ways you can control it.
-
Mar 21st, 2024, 04:23 PM
#3
Re: Everything is Nullable???
Interesting. Not really liking it, but I do understand the point.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|