in C# before the Nullable(?) was invented,Which can be null?which can't be null?

兰树豪 381 Reputation points
2023-03-23T08:43:50.02+00:00

in C# before the Nullable(?) was invented,Which can be null?which can't be null?

and Why are we need to worried about null or not null?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,291 questions
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 26,141 Reputation points
    2023-03-23T11:41:35.51+00:00

    in C# before the Nullable(?) was invented...and Why are we need to worried about null or not null?

    The default value of an uninitialized int is zero which is a valid integer value. But what if you need to validate a int that was entered by a user or passed to a service endpoint by code? That's where nullable types are handy. An int? can be null.

    The C# reference documentation illustrates the concept with code examples.

    Nullable value types (C# reference)

    Which can be null?which can't be null?

    The default value of a reference type is null while the default value of a value type depends on the value type. An int is zero. A bool is false...

    Value types (C# reference)

    Reference types (C# reference)

    0 comments No comments

  2. Bruce (SqlWork.com) 56,846 Reputation points
    2023-03-23T18:28:31.24+00:00

    in general null means the value is unknown or undefined.

    in C# value types can not be null as value types are stack storage (a value on the stack) which is accessed directly, vs heap storage which is accessed via a pointer.

    the C# design of value types did not support a null indicator (which would take extra memory)

    although value types can be boxed (stored on the heap) the runtime did not support a null case.

    so if you have a bool variable (a value type) it can be true or false, but not null. but what if it was important to know in your code that the value was unknown? this is the use case nullable types where added to support.

    Nullable<bool> is an object that support true, false and null. (bool? is just syntax sugar). to use a nullable bool as a bool, it must be cast as a bool:

    bool? a = null; 
    bool b = (bool) a;   // runtime error because a is null 
    bool c = a ?? false; // cast with default value
    bool d = a;          // compile error 
    bool e = null;       // compile error
    
    0 comments No comments

  3. YujianYao-MSFT 4,281 Reputation points Microsoft Vendor
    2023-03-24T07:37:33.5366667+00:00

    Hi 兰树豪,

    The replies from Bruce and AgaveJoe are very useful, and I recommend you read them carefully. I made some additions you could also refer to it.

    As a value type is implicitly convertible to the corresponding nullable value type, you could assign a value to a variable of a nullable value type as you would do that for its underlying value type.

    bool? flag = null;
    // An array of a nullable value type:
    int?[] arr = new int?[10];
    

    You could use the is operator with a type pattern to both examine an instance of a nullable value type for null and retrieve a value of an underlying type:

    int?a = 42;
    if (a is null)
    {
        Console.WriteLine($" a does not have a value");
    }
    else
    {
        Console.WriteLine($"a is {a.Value}");
    }
    

    Regarding the knowledge of null in C#, I highly recommend you read this issue, I believe it will be helpful to you.

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments