I use types Nullable<T>quite a lot when loading values into classes and structures when I need them to be nullable, for example, loading a value with a null value from the database (as an example below).
Consider this piece of code:
public class InfoObject
{
public int? UserID { get; set; }
}
SqlInt32 userID = reader.GetSqlInt32(reader.GetOrdinal("intUserID"));
When I need to load a value into a nullable property, sometimes I do this:
infoObject.UserID = userID.IsNull ? (int?)null : userID.Value;
Sometimes I do this instead:
infoObject.UserID = userID.IsNull ? new int?() : userID.Value;
Although they achieve the same result, I would like to know if anyone knows what is best used between (int?)nulland new int?()in terms of performance, least IL code, best practices, etc.?
As a rule, I approved the version of the new int?()above code, but I’m not sure if there is (int?)nullmore than faster execution for the compiler new int?().
Hurrah!