Joe,
Why not just get the Value on the nullable to begin with? It will
return a default value if it is "null" (meaning, it is flagged as null, it
isn't actually null).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
(E-Mail Removed)
"Joe Bloggs" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>