Rectangle = null?

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

    #1

    Rectangle = null?

    Why can't I say the following;

    Rectangle r = null;

    The compiler tells me that null is a value type and can't be converted
    to Rectangle. But I can't generally set any reference type to null.
    For example, I can say:

    MyClass c = null;

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Rectangle = null?

    Dom,

    I think you mean that you can generally set any reference type to null
    (you said can't). This is true, you can assign null to reference type
    variables.

    However, in this case, the compiler is telling you the truth. Rectangle
    is a value type, not a reference type, and you can't assign null to it
    (assuming that this is the Rectangle structure from the System.Drawing
    namepspace).

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Dom" <dolivastro@gma il.comwrote in message
    news:1187020658 .296230.128640@ k79g2000hse.goo glegroups.com.. .
    Why can't I say the following;
    >
    Rectangle r = null;
    >
    The compiler tells me that null is a value type and can't be converted
    to Rectangle. But I can't generally set any reference type to null.
    For example, I can say:
    >
    MyClass c = null;
    >

    Comment

    • Olie

      #3
      Re: Rectangle = null?

      On Aug 13, 4:57 pm, Dom <dolivas...@gma il.comwrote:
      Why can't I say the following;
      >
      Rectangle r = null;
      >
      The compiler tells me that null is a value type and can't be converted
      to Rectangle. But I can't generally set any reference type to null.
      For example, I can say:
      >
      MyClass c = null;
      A Rectangle is not a class it is a structure and so you can not assign
      a null value to it. You should use instead.

      Rectangle c = Rectangle.Empty ;

      Comment

      • Dom

        #4
        Re: Rectangle = null?

        On Aug 13, 11:57 am, Dom <dolivas...@gma il.comwrote:
        Why can't I say the following;
        >
        Rectangle r = null;
        >
        The compiler tells me that null is a value type and can't be converted
        to Rectangle. But I can't generally set any reference type to null.
        For example, I can say:
        >
        MyClass c = null;
        Thanks, makes sense now. And Yes, I did mean "I *can* generally ...".

        Is there any specific reason why rectangle is a structure, not a
        class. And how would I know this from the intellisense window, or is
        it just something you learn.

        Dom

        Comment

        • Olie

          #5
          Re: Rectangle = null?

          On Aug 13, 5:27 pm, Dom <dolivas...@gma il.comwrote:
          On Aug 13, 11:57 am, Dom <dolivas...@gma il.comwrote:
          >
          Why can't I say the following;
          >
          Rectangle r = null;
          >
          The compiler tells me that null is a value type and can't be converted
          to Rectangle. But I can't generally set any reference type to null.
          For example, I can say:
          >
          MyClass c = null;
          >
          Thanks, makes sense now. And Yes, I did mean "I *can* generally ...".
          >
          Is there any specific reason why rectangle is a structure, not a
          class. And how would I know this from the intellisense window, or is
          it just something you learn.
          >
          Dom
          Traditionally structures are used instead of classes because they
          require less resources. I am not sure if that is the reason here
          though. In fact the line between structures and classes is very
          blurred in .net.

          You can tell if it is a structure and not a class in the intelli-sense
          window because it has a slightly different icon.

          Comment

          Working...