Results 1 to 3 of 3

Thread: [RESOLVED] Everything is Nullable???

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,151

    Resolved [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

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,545

    Re: Everything is Nullable???

    Quote Originally Posted by Shaggy Hiker View Post
    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.

  3. #3

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,151

    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
  •  



Click Here to Expand Forum to Full Width