How can i get 0 as integer value from (int)null
.
EDIT 1: I want to create a function that will return me default values for null representa
A generic method that returns a cast instance of an object or the default value could be implemented as follows:
static T Cast(object value) {
if (value is T)
return (T)value;
else
return default(T);
}
This way, using a valid value will yield the value itself:
int value = Cast(4); //value = 4
and a null value will get the default:
int value = Cast(null); //value = 0
Notice that since the method takes object as an argument, this will cause boxing when used with struct objects (like int).