none
Is this true that Nullable<> types are user defined type? RRS feed

Answers

All replies

  • Hello,

    If you mean say 

    int counter;

    then do

    int? counter;

    Yes they are user defined.

    See also for more in depth details

    https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1?view=netframework-4.8#remarks 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Reza1992 Wednesday, February 26, 2020 2:42 PM
    Wednesday, February 26, 2020 11:27 AM
    Moderator
  • Generics type are specials as they can represent value types (struct) and references types (class). This means you cannot use the T? type to represent a nullable type as it would conflict with the existing Nullable<T> when the generic is a value type

    https://www.meziantou.net/csharp-8-nullable-reference-types.htm

    Wednesday, February 26, 2020 1:58 PM
  • Hi again

    Is this true that Nullable<> types are user defined type?

    I'm just plain confused by Karen's answer.

    Anything that's declared as a CLASS is a reference and therefore nullable type.

    Anything that's declared as a STRUCT is a value and therefore non-nullable type.

    It doesn't matter if the CLASS or STRUCT is pre-baked into DotNET Framework or User-Defined.  Classes are reference/nullable types and Structs are value/non-nullable types.

    All the examples in Karen's post (which is marked as answer here) are INT, which is STRUCT, which is NON-NULLABLE.

    Wednesday, February 26, 2020 3:56 PM

  • I'm just plain confused by Karen's answer.


    All the examples in Karen's post (which is marked as answer here) are INT, which is STRUCT, which is NON-NULLABLE.

    Did you miss the ? in "int?" ?

    int counter1 = null; // error - int is value type that can't be null
    
    int? counter2 = null; // OK - int? is a nullable int
    

    Nullable value types (C# reference)
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types

    - Wayne

    Wednesday, February 26, 2020 5:15 PM
  • Thanks, Wayne.  I did miss that - actually never seen it before.  Now I have some reading to do :-)
    Wednesday, February 26, 2020 7:14 PM