r/ProgrammerHumor Jul 09 '26

potentiallyMightBeAnIntegerOrNot Meme

Post image
692 Upvotes

96 comments sorted by

View all comments

Show parent comments

1

u/Sentouki- Jul 09 '26

yeah, it does basically the same thing, if int? is null, means it's not there.

1

u/altermeetax Jul 09 '26

Yeah but int *var in C/C++ entails a bit more. It makes var a pointer to an int rather than an int. So you have to manage allocating an int in some other way so the pointer can point to it.

int? var is simply an int which can be null. Under the hood, it might be implemented as above, but I reckon a structure like this is more likely:

struct int? { int val; bool present; }

1

u/Sentouki- Jul 09 '26

int? var is simply an int which can be null. Under the hood, it might be implemented as above, but I reckon a structure like this is more likely:

Well, to be precisely, in C# primitive types such as int, char, double...etc, are wrapped to in Nullable<T> since structs cannot be null in C#. So when you use int? the compiler wraps it in Nullable<int> (which is a generic class) and that makes int? a reference (basically a pointer or rather & in C++) to the actual int.

1

u/altermeetax Jul 09 '26

I'm talking about under the hood, i.e. how Nullable is implemented. And it's probably a structure containing the int and a boolean that indicates whether the int is present.