using nullable types in generics

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    using nullable types in generics

    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

  • Jon Skeet [C# MVP]

    #2
    Re: using nullable types in generics

    On Sep 26, 9:01 am, "Tony Johansson" <t.johans...@lo gica.comwrote:
    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

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: using nullable types in generics

      On Sep 26, 4:01 am, "Tony Johansson" <t.johans...@lo gica.comwrote:
      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

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: using nullable types in generics

        On Sep 26, 4:20 pm, "Ignacio Machin ( .NET/ C# MVP )"
        <ignacio.mac... @gmail.comwrote :
        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.
        Slight correction (related to your question later) - Nullable<Tis a
        struct, not a class.
        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

        Comment

        Working...