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
You can use the Nullable structure
int value = new Nullable().GetValueOrDefault();
You can also use the default
keyword
int value = default(int);
Following 2nd edit:
You need a function that receive any type of parameter, so object will be used. Your function is similar to the Field
extension method on a DataRow
public static T GetValue(object value)
{
if (value == null || value == DBNull.Value)
return default(T);
else
return (T)value;
}
Using that function, if you want an int (and you expect value to be an int) you call it like:
int result = GetValue(dataRow["Somefield"]);