473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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)).GetGeneri cArguments()[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.
Jul 17 '06 #1
1 1293
"Joe Bloggs" <es***@rocketmail.coma écrit dans le message de news:
44***********************@per-qv1-newsreader-01.iinet.net.au...

| Now, how do I modify the constraint so that I can accept either value
| types or Nullable types?

I'm not sure what you are trying to achieve here but have you tried the
following code ?

public static object GetSafeValue<T>(Nullable<Tvalue) where T : struct
{
return value.GetValueOrDefault(default(T));
}

static void Main(string[] args)
{
int? x = 5;
bool? i = null;
Console.WriteLine(x.GetType().FullName);
Console.WriteLine(" x={0}", GetSafeValue(x));
Console.WriteLine(" i={0}", GetSafeValue(i));
Console.ReadLine();
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jul 17 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Wood | last post by:
I was just looking at an article about using nullable value types. (value types that can effectively have no value and not be set). The syntax is to append a question-mark to the value type in...
2
by: julio | last post by:
I need to assign null values to datetime fields Where I can find types like NullableDateTime or NullableInt. TIA
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
6
by: Dan Holmes | last post by:
I have a class that i need a constraint of int, string, float or bool. I have tried the following but can't make VS accept it. I read the docs and they showed that any value type can be used...
1
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
2
by: Joe Bloggs | last post by:
Hi, compiling the following code: public class App { static void Main() { int? x = 5; bool? i = null;
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
3
by: Mike P | last post by:
What are nullable types used for? The only use I can think of is where I have a method like this where some values being passed to it may be null : public int AddUser(int? UserRegion, string...
6
by: Tony Johansson | last post by:
Hello! I'm reading in a book called Visual C# 2005. In the chapter about Generics there ia a section about Nullable types. Here is the text that isn't complete true I think. It says: "You...
3
by: Tony Johansson | last post by:
Hello! Is it possible to declare existing .net generics to have nullable types(for example type int? ) ? If the answer on the previous question is yes then I assume that you can also define...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.