Re: Boxing and Unboxing of Value-Types when passed to a method call

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Bruce Wood (brucewood_at_canada.com)
Date: 01/24/05


Date: 24 Jan 2005 11:51:38 -0800

I believe that this would act the same as passing an int as int* in C.
To wit:

Whether there is heap space allocated depends very much on where the
"int a = 1355" appears. If it is a local variable in a method then it
will be on the stack; if it is a class member then it will allocated
within the class data area allocated on the heap; if it is a struct
member then it will be allocated within the struct data area either on
the stack or the heap, depending on whether the struct is a local
variable or a class member.

myMethod will be passed a pointer to (reference to) the memory location
at which the integer called "a" is stored. The pointer (reference) will
be allocated on the stack, because it is a method argument. Modifying b
within myMethod will modify a, because b and a refer to the same memory
location.

No boxing or unboxing involved; the reference passed to myMethod points
directly to the integer.



Relevant Pages

  • Re: Boxing and Unboxing ??
    ... They are on the heap to avoid lifetime issues. ... int *retrieve ... instead of passed by reference. ... overhead, merely boxing initialization. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Boxing and Unboxing of Value-Types when passed to a method call
    ... void myMethod ... In this case, myMethod is expecting an object, which is a reference ... and since a reference to an instance on the heap is the "lowest common ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ref parameter for any object
    ... create a new instance of object in the heap. ... heap the int. ... what happen is that with a ref parameter you cannot do this, ... this is needed cause otherwise the reference used is lost at return, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: int number = new int();
    ... a copy of this actual value number is put on the heap. ... If you box an int like. ... // here number is still a value type of type Int32 but is ... reference number which is stored on the heap. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: int number = new int();
    ... If you box an int like. ... reference number which is stored on the heap. ... the assignment. ...
    (microsoft.public.dotnet.languages.csharp)