PC Review


Reply
Thread Tools Rate Thread

How to add a constraint of Nullable Types to a generics method

 
 
Joe Bloggs
Guest
Posts: n/a
 
      17th Jul 2006
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.

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      17th Jul 2006
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.
>



 
Reply With Quote
 
Barry Kelly
Guest
Posts: n/a
 
      17th Jul 2006
"Joe Bloggs" <(E-Mail Removed)> wrote:

> public static object GetSafeValue<T, N>(T value) where T:struct


Yes, struct excludes nullable types.

> 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?


You can't do it statically. You'll have to do it at runtime.

-- Barry

--
http://barrkel.blogspot.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using nullable types in generics Tony Johansson Microsoft C# .NET 3 26th Sep 2008 05:10 PM
Generics constraint for nullable or reference types Sam Kong Microsoft C# .NET 8 31st Aug 2006 04:46 PM
How to add a constraint of Nullable Types to a generics method Joe Bloggs Microsoft Dot NET 1 17th Jul 2006 08:31 PM
Nullable types and generics and how they go through SOAP in VS2005... Dave A Microsoft Dot NET Framework 0 10th Jan 2005 12:45 PM
nullable types. why no support for nullable string Sakoulakis Microsoft C# .NET 3 6th Aug 2004 03:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:38 PM.