I’d like to have an integer variable which can be set to null and don’t want to have to use the int? myVariable syntax. I tried using int and Int16 to no avail. Do I have to use int? myVariable?
I mentioned this because in Java there is both an ‘int’ type (a primitive) and ‘Integer’ (a reference type). I wanted to be sure that there isn’t a built-in integer reference type that I could be using. I’ll use ‘int?’ for what I’m doing.
For info,
int?/Nullable<T>is not a reference-type; it is simply a “nullable type”, meaning: a struct (essentially andintand aboolflag) with special compiler rules (re null checks, operators, etc) and CLI rules (for boxing/unboxing). There is no “integer reference-type” in .NET, unless you count boxing:but this creates an unnecessary object and has lots of associated other issues.
For what you want,
int?should be ideal. You could use the long-hand syntax (Nullable<int>) but IMO this is unnecessarily verbose, and I’ve seen it confuse people.