Variable type ending with ?

前端 未结 7 2071
别跟我提以往
别跟我提以往 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 11:46

    The ? operator indicates that the property is in fact a nullable type.

    public bool? Verbose { get; set; } 
    

    is equilvalent to

    public Nullable Verbose { get; set; }
    

    A nullable type is a special type introduced in c# 2.0 which accepts a value type as a generic praramater type and allow nulls to be assigned to the type.

    The nullable type only accept value types as generic arguments which is why you get a compile error when you try to use the ? operator in conjunction with the string type.

    For more information: MSDN Nullable Types

提交回复
热议问题