C
C#4mo ago
Dankh917

✅ 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
Angius
Angius4mo ago
Yes, T? means the type is nullable int? can be an int or null for example
int a = 0; // valid
int b = null; // invalid
int? c = 0; // valid
int? d = null; // valid
int a = 0; // valid
int b = null; // invalid
int? c = 0; // valid
int? d = null; // valid
Jimmacle
Jimmacle4mo ago
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
Dankh917
Dankh917OP4mo ago
I see, thanks I see , thank you
Angius
Angius4mo ago
No description
Dankh917
Dankh917OP4mo ago
thank you , saving this one 😅
Jimmacle
Jimmacle4mo ago
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
Dankh917
Dankh917OP4mo ago
thats an importent one, I"ll keep that in mind
Want results from more Discord servers?
Add your server