using nullable types in generics

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top