Home > Software engineering >  Why that code doesn't setting null to value
Why that code doesn't setting null to value

Time:10-23

I can not understood why this code doesn't changing value from null(compiler even can't see that null instead of that Im reciving blank spots)

 public string? ClubName
        {
            get => _clubname;

            set
            {
                _clubname = value ?? "it";
            }
        } 

enter image description here

CodePudding user response:

Your setter uses the null-coalescing operator ??, which substitutes "it" should value be null:

_clubname = value ?? "it";

So, you can never assign null to the property via the setter.

However, your property is backed by this private field:

_string clubname;

which the compiler initializes to null.

While you can never set null, the code in your screenshot never assigns a value, so the initial value remains.

  • Related