Re: TextBox.text = int (does boxing happen here)
Tech-Archive recommends: Fix windows errors by optimizing your registry
Boxing does not occur, but you must convert the int to a string. Here
is some code that assigns an int to a textbox.text property:
int theInteger = 25;
textBox1.Text = Convert.ToString(theInteger);
And here is the IL generated: There is no boxing involved.
// Code Size: 21 byte(s)
.maxstack 2
.locals init (
int32 num1)
L_0000: ldc.i4.s 25
L_0002: stloc.0
L_0003: ldarg.0
L_0004: ldfld [System.Windows.Forms]System.Windows.Forms.TextBox
TestCs.Form1::textBox1
L_0009: ldloc.0
L_000a: call string [mscorlib]System.Convert::ToString(int32)
L_000f: callvirt instance void
[System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
L_0014: ret
.
Relevant Pages
- Re: Value Types and Reference Types
... So your saying that an int is not special? ... What I mean is that the compiler ... Boxing definitely creates an object on the heap. ... Int32 x; are pretty much identical except for speed/memory issues. ... (microsoft.public.dotnet.languages.csharp) - Re: Roll your own std::vector ???
... I assumed boxing occurred here. ... string ConvertToString ... int i = 123; ... Or you could use generics to simplify this even further: ... (microsoft.public.dotnet.languages.csharp) - Re: Roll your own std::vector ???
... I assumed boxing occurred here. ... string ConvertToString ... int i = 123; ... The compîler infers the type of the generic method from the type being ... (microsoft.public.dotnet.languages.csharp) - Re: Generics + D2007/win32
... int j = o; ... The first assignment of i to o does something called boxing. ... an Int32 object is created and the int value is placed inside it; ... you incur these boxing penalties. ... (borland.public.delphi.non-technical) - Re: Value Types and Reference Types
... So your saying that an int is not special? ... What I mean is that the compiler ... but the main point was that boxing is slow. ... Actually a boxing operation takes 11 machine instruction, 10 instruction to allocate the object space on the heap and 1 instruction to move the value into the object. ... (microsoft.public.dotnet.languages.csharp) |
|