Re: int number = new int();

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



Hello!

I just wonder if it's then correct to say that
Int32 is a structure they never are placed on the heap.
I mean when you are boxing a copy is actually stored on the heap
as I described

//Tony


"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message news:73b37d53-7e92-4e35-b5d8-f5d96ce40be2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Sep 26, 2:28 pm, "Tony Johansson" <t.johans...@xxxxxxxxxx> wrote:
Assume you have the following.
int number = 1;
object o = number;
If you here change the value of number it won't change what o is refering to
because that is stored on the heap.

Yes.

So we have one value of number which is stored on the stack and when we do
object o = number
a copy of this actual value number is put on the heap.

Yes.

If we now change number by using this statement
number = 2;
the number that o is refering to is still 1;

Correct.

I almost 99.99 % sure that this is correct.

Absolutely. I was merely correcting this statement that you made
before:

// so o is stored on the stack and will
// reference number which is stored on the heap.

"o" doesn't reference "number". It reference an object which was
created with a value which happened to be the value of "number" at the
time. The two variables are independent immediately after the
assignment.

Jon

.



Relevant Pages

  • Re: int number = new int();
    ... int number = 1; ... If you here change the value of number it won't change what o is refering to ... a copy of this actual value number is put on the heap. ... assignment. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ArrayList vs. List<>
    ... object on the heap containing that value, ... None of that happens when you're using a reference type to start with. ... reference-to-reference conversions being described as boxing before.) ... Do you mean Jon Davis' statement? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Struct inside class
    ... In this case, boxing must be done. ... that the compiler will have to generate addition calls into the memory ... so it's stored on the heap. ... If GC_ALLOCATE can't allocate the requested ...
    (microsoft.public.dotnet.framework)
  • Boxing effect on struct methods
    ... Couple of questions relating to boxing. ... the heap so that the system can treat a value type like a reference type. ... If I add custom instance method on a struct, will it box that type each ...
    (microsoft.public.dotnet.languages.csharp)
  • Boxing questions relating to struct methods
    ... Couple of questions relating to boxing. ... the heap so that the system can treat a value type like a reference type. ... If I add custom instance method on a struct, will it box that type each ...
    (microsoft.public.dotnet.framework)