Boxing and implicit casting to System.Object type

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

    #1

    Boxing and implicit casting to System.Object type

    Hi !

    Jeffrey Richter gives the following example in his book:

    struct Point {
    Int32 x, y;
    }
    class AAA{
    ....
    public virtual void Add(Object value);
    ....
    }

    We want to pass the variable of Point type to add method.

    Now Richter writes:
    From this, you can plainly see that Add takes an Object as a
    parameter, indicating that Add requires a reference (or pointer) to an
    object on the managed heap as a parameter. But in the preceding code,
    I’m passing p a Point value type. For this code to work, the
    Point value type must be converted into a true heap-managed object and
    a reference to this object
    must be obtained.


    My queation is: how we know from this code that Add expects reference
    type ?
    Is it true when I modify the code as follows ? :

    public virtual void Add(System.Valu eType value);

    Thanks

    Jean
  • Mattias Sjögren

    #2
    Re: Boxing and implicit casting to System.Object type

    Jean,
    [color=blue]
    >My queation is: how we know from this code that Add expects reference
    >type ?[/color]

    Because System.Object is a reference type.

    [color=blue]
    >Is it true when I modify the code as follows ? :
    >
    > public virtual void Add(System.Valu eType value);[/color]

    Yes, ValueType is also a reference type (though everything derived
    from ValueType, except for Enum, is a value type).



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Boxing and implicit casting to System.Object type

      Mattias,[color=blue]
      > Yes, ValueType is also a reference type (though everything derived
      > from ValueType, except for Enum, is a value type).[/color]
      Just for my own clarification:

      Do you mean System.Enum itself, or the types that I've defined to be enums;

      I've always considered types that I've defined to be enums to be value
      types. I can see that System.Enum itself acts more like a reference type
      just like System.ValueTyp e.

      I consider: System.TypeCode (an enum) to be a value type, while System.Enum
      would be a reference type. Am I wrong?

      Thanks
      Jay

      "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
      news:ufnyE7IdDH A.3948@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Jean,
      >[color=green]
      > >My queation is: how we know from this code that Add expects reference
      > >type ?[/color]
      >
      > Because System.Object is a reference type.
      >
      >[color=green]
      > >Is it true when I modify the code as follows ? :
      > >
      > > public virtual void Add(System.Valu eType value);[/color]
      >
      > Yes, ValueType is also a reference type (though everything derived
      > from ValueType, except for Enum, is a value type).
      >
      >
      >
      > Mattias
      >
      > --
      > Mattias Sjögren [MVP] mattias @ mvps.org
      > http://www.msjogren.net/dotnet/
      > Please reply only to the newsgroup.[/color]


      Comment

      • Mattias Sjögren

        #4
        Re: Boxing and implicit casting to System.Object type

        Hi Jay,
        [color=blue]
        >Do you mean System.Enum itself, or the types that I've defined to be enums;[/color]

        System.Enum itself.

        [color=blue]
        >I consider: System.TypeCode (an enum) to be a value type, while System.Enum
        >would be a reference type. Am I wrong?[/color]

        No, you're definitely right.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org

        Please reply only to the newsgroup.

        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: Boxing and implicit casting to System.Object type

          Mattias,
          Thanks, I was 'worried' for a second ;-)

          Jay

          "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
          news:%23AX1X0Jd DHA.2508@tk2msf tngp13.phx.gbl. ..[color=blue]
          > Hi Jay,
          >[color=green]
          > >Do you mean System.Enum itself, or the types that I've defined to be[/color][/color]
          enums;[color=blue]
          >
          > System.Enum itself.
          >
          >[color=green]
          > >I consider: System.TypeCode (an enum) to be a value type, while[/color][/color]
          System.Enum[color=blue][color=green]
          > >would be a reference type. Am I wrong?[/color]
          >
          > No, you're definitely right.
          >
          >
          >
          > Mattias
          >
          > --
          > Mattias Sjögren [MVP] mattias @ mvps.org
          > http://www.msjogren.net/dotnet/
          > Please reply only to the newsgroup.[/color]


          Comment

          • Jean Stax

            #6
            Re: Boxing and implicit casting to System.Object type

            I am still confused...
            In .NET every type directly or indirectly is derived from
            System.Object. According to what you are sying the System.Object is
            reference type. However, System.ValueTyp e (which is a base type for
            all vlue types) is derived from System.Object (which is reference
            type).
            Is it not a little odd ?

            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message news:<O$wFF9JdD HA.1460@TK2MSFT NGP10.phx.gbl>. ..[color=blue]
            > Mattias,
            > Thanks, I was 'worried' for a second ;-)
            >
            > Jay
            >
            > "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
            > news:%23AX1X0Jd DHA.2508@tk2msf tngp13.phx.gbl. ..[color=green]
            > > Hi Jay,
            > >[color=darkred]
            > > >Do you mean System.Enum itself, or the types that I've defined to be[/color][/color]
            > enums;[color=green]
            > >
            > > System.Enum itself.
            > >
            > >[color=darkred]
            > > >I consider: System.TypeCode (an enum) to be a value type, while[/color][/color]
            > System.Enum[color=green][color=darkred]
            > > >would be a reference type. Am I wrong?[/color]
            > >
            > > No, you're definitely right.
            > >
            > >
            > >
            > > Mattias
            > >
            > > --
            > > Mattias Sjögren [MVP] mattias @ mvps.org
            > > http://www.msjogren.net/dotnet/
            > > Please reply only to the newsgroup.[/color][/color]

            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Boxing and implicit casting to System.Object type

              Jean,
              Does this article help?


              [color=blue]
              > Is it not a little odd ?[/color]
              I don't think so, It has to do with the importance (ease of use) of having
              every thing inherit from System.Object, plus still have actual value types.
              Imagine the amount of memory used, if integers actually existed on the heap
              as reference types! Can you say desperate need of the flyweight pattern
              (which in itself would cause overhead).

              As far as I can tell, the reason System.ValueTyp e & System.Enum behave more
              like a reference type is that when you call a routine with these two as
              parameters, the value needs to be boxed as you have a variable amount of
              data coming in. (byte is not the same size as long).

              Hope this helps
              Jay


              "Jean Stax" <jean_stax@hotm ail.com> wrote in message
              news:df5c1ee6.0 309070009.76fd4 771@posting.goo gle.com...[color=blue]
              > I am still confused...
              > In .NET every type directly or indirectly is derived from
              > System.Object. According to what you are sying the System.Object is
              > reference type. However, System.ValueTyp e (which is a base type for
              > all vlue types) is derived from System.Object (which is reference
              > type).
              > Is it not a little odd ?
              >
              > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in[/color]
              message news:<O$wFF9JdD HA.1460@TK2MSFT NGP10.phx.gbl>. ..[color=blue][color=green]
              > > Mattias,
              > > Thanks, I was 'worried' for a second ;-)
              > >
              > > Jay
              > >
              > > "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
              > > news:%23AX1X0Jd DHA.2508@tk2msf tngp13.phx.gbl. ..[color=darkred]
              > > > Hi Jay,
              > > >
              > > > >Do you mean System.Enum itself, or the types that I've defined to be[/color]
              > > enums;[color=darkred]
              > > >
              > > > System.Enum itself.
              > > >
              > > >
              > > > >I consider: System.TypeCode (an enum) to be a value type, while[/color]
              > > System.Enum[color=darkred]
              > > > >would be a reference type. Am I wrong?
              > > >
              > > > No, you're definitely right.
              > > >
              > > >
              > > >
              > > > Mattias
              > > >
              > > > --
              > > > Mattias Sjögren [MVP] mattias @ mvps.org
              > > > http://www.msjogren.net/dotnet/
              > > > Please reply only to the newsgroup.[/color][/color][/color]


              Comment

              • Mattias Sjögren

                #8
                Re: Boxing and implicit casting to System.Object type

                Jean,
                [color=blue]
                >Is it not a little odd ?[/color]

                You might want to read





                Mattias

                --
                Mattias Sjögren [MVP] mattias @ mvps.org

                Please reply only to the newsgroup.

                Comment

                Working...