Variable type ending with ?

前端 未结 7 2141
别跟我提以往
别跟我提以往 2021-01-04 11:08

What does ? mean:

public bool? Verbose { get; set; }

When applied to string?, there is an error:

7条回答
  •  臣服心动
    2021-01-04 12:04

    The ? is shorthand for the struct below:

    struct Nullable
    {
        public bool HasValue;
        public T Value;
    }
    

    You can use this struct directly, but the ? is the shortcut syntax to make the resulting code much cleaner. Rather than typing:

    Nullable x = new Nullable(125);
    

    Instead, you can write:

    int? x = 125;
    

    This doesn't work with string, as a string is a Reference type and not a Value type.

提交回复
热议问题