TextBox.text = int (does boxing happen here)

  • Thread starter Thread starter Tariq
  • Start date Start date
T

Tariq

This might sound like a stupid question, but anywas

when you assign an int to a textbox via the textbox.text propperty I
assume boxing happens. If this assigment happend too often rapidly on a
GUI wont it be a performance step back? Is there a more efficient way
of assigning other value types to a textbox?
 
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
 
TextBox.Text is a string, not an object. Strings don't box.

If you want to attach something besides a string to a TextBox, you can
always use the Tag property.

Pete
 
Back
Top