int? is syntactic sugar for Nullable<int> ; as for Nullable<T> if we look at its implementation
https://referencesource.microsoft.com/#mscorlib/system/nullable.cs,ffebe438fd9cbf0e
we will find the implicit expression of the operator:
public struct Nullable<T> where T : struct { ... [System.Runtime.Versioning.NonVersionable] public static implicit operator Nullable<T>(T value) { return new Nullable<T>(value); } ... }
So, for any struct T instead of calling the constructor explicitly
T value = ... T? test = new Nullable<T>(value);
we can use an implicit operator
T? test = value; // implicit operation in action
In your particular case, T is int , and we have
int? foo = 5;
source share