J
Joe Bloggs
Hi,
compiling the following code:
public class App
{
static void Main()
{
int? x = 5;
bool? i = null;
Console.WriteLine(GetType(x).FullName);
Console.WriteLine(" x={0}", GetSafeValue(x));
Console.WriteLine(" i={0}", GetSafeValue(i));
Console.ReadLine();
}
public static object GetSafeValue<T, N>(T value) where T:struct
{
if (!IsNullableValueType(GetType(value)))
{
Console.WriteLine(" {0} is not a Nullable type",
value.GetType());
return value;
}
object nullableValue = value;
return nullableValue ??
GetDefaultDataTypeValue((GetType(value)).GetGenericArguments()[0]);
}
}
I get the error message
"The type 'int?' must be a non-nullable value type in order to use it
as parameter 'T' in the generic type or method
'App.GetSafeValue<T>(T)'"
Obviously, I have set the constraint to only accept value types.
Now, how do I modify the constraint so that I can accept either value
types or Nullable types?
Thanks in advance.
compiling the following code:
public class App
{
static void Main()
{
int? x = 5;
bool? i = null;
Console.WriteLine(GetType(x).FullName);
Console.WriteLine(" x={0}", GetSafeValue(x));
Console.WriteLine(" i={0}", GetSafeValue(i));
Console.ReadLine();
}
public static object GetSafeValue<T, N>(T value) where T:struct
{
if (!IsNullableValueType(GetType(value)))
{
Console.WriteLine(" {0} is not a Nullable type",
value.GetType());
return value;
}
object nullableValue = value;
return nullableValue ??
GetDefaultDataTypeValue((GetType(value)).GetGenericArguments()[0]);
}
}
I get the error message
"The type 'int?' must be a non-nullable value type in order to use it
as parameter 'T' in the generic type or method
'App.GetSafeValue<T>(T)'"
Obviously, I have set the constraint to only accept value types.
Now, how do I modify the constraint so that I can accept either value
types or Nullable types?
Thanks in advance.