using nullable types in generics

T

Tony Johansson

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 my own generics class
having nullable types.

//Tony
 
J

Jon Skeet [C# MVP]

Is it possible to declare existing .net generics to have nullable types(for
example type int? ) ?

Assuming you mean this:

List<int?> x = new List<int?>();

Then yes.
If the answer on the previous question is yes then I assume that you can
also define my own generics class having nullable types.

I'm not entirely sure what you mean here, but generally speaking, yes.

Jon
 
I

Ignacio Machin ( .NET/ C# MVP )

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 my own generics class
having nullable types.

//Tony

Hi,

Nullable types are just as regular types except that the compiler
create a wrap class around them. For example int? it create a class
that have two properties, HasValue and Value.
So the answer for your question is yes. You can use a nullable type as
any other type.

One question I have now is if the nullable type is a reference or a
value type?
I would have to check the docs
 
J

Jon Skeet [C# MVP]

Nullable types are just as regular types except that the compiler
create a wrap class around them. For example int? it create a class
that have two properties, HasValue and Value.

So the answer for your question is yes. You can use a nullable type as
any other type.

One question I have now is if the nullable type is a reference or a
value type?
I would have to check the docs

It's a struct - otherwise there'd be relatively little value in having
it instead of having explicit access to the boxed types.

There's one odd thing about Nullable<T> - it won't satisfy either "T :
class" or "T : struct" generic constraints.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top