✅ Q about the "?" symbol when declaring vars
Hello everybody ,I have seen some c# vidoes where people declare variables with "?", for example "int?","bool?" , I have asked copilot what does it mean and he said that its just "normal variables" but they are special in that they can also store Null .
my question is : how accurate is his answer and in what case/context will I use :"type?" instead of "type"
thanks
7 Replies
Yes,
T?
means the type is nullable
int?
can be an int or null
for example
they're useful when you need to explicitly store "nothing" as opposed to the default value
like if 0 actually means something to your program you wouldn't want to use it to also mean no value
I see, thanks
I see , thank you
thank you , saving this one 😅
keep in mind that value types and reference types work a bit differently for nullability
since structs can't actually be null, the nullable versions are a special type that isn't the same as the regular one
e.g. int? is actually Nullable<int> and the way you access the values or check for null can be a bit different
thats an importent one, I"ll keep that in mind