Variable type ending with ?

前端 未结 7 2067
别跟我提以往
别跟我提以往 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<T>
    {
        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<int> x = new Nullable<int>(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.

    0 讨论(0)
提交回复
热议问题