469,630 Members | 1,222 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,630 developers. It's quick & easy.

Which is better?

With respect to performance, is either of these better?

Dim myTextBox As New TextBox

or

Dim myTextBox As TextBox
myTextBox = New TextBox

or

Dim myTextBox As TextBox = New TextBox
Nov 18 '05 #1
3 1112
It's unlikely you'll ever see a difference (you could even see if it doesn't
create the same underlying MSIL code).

This is the same (unlike VB6 were New was checking for existence at each
access to the variable). 2 may be usefull as it allows to create an object
only when (and in particular *if*) you need it which could could save some
time (under unusual circumstances though it's likely best to avoid creating
something if you don't need it).

As a side note it's likely most often considered better to code for clarity
and to work on identified performance problem rather than to be picky about
few microseconds gains... For example if you use a DB it's likely better to
start by looking at indexes design...
Patrice

--

"Paul" <pa*******@smmoving.com> a écrit dans le message de
news:%2****************@TK2MSFTNGP11.phx.gbl...
With respect to performance, is either of these better?

Dim myTextBox As New TextBox

or

Dim myTextBox As TextBox
myTextBox = New TextBox

or

Dim myTextBox As TextBox = New TextBox

Nov 18 '05 #2
Numbers 1 and 3 are better in terms of performance (not sure if either of
them is better than the other). However, it is minimal.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

"Paul" <pa*******@smmoving.com> wrote in message
news:#1**************@TK2MSFTNGP11.phx.gbl...
With respect to performance, is either of these better?

Dim myTextBox As New TextBox

or

Dim myTextBox As TextBox
myTextBox = New TextBox

or

Dim myTextBox As TextBox = New TextBox

Nov 18 '05 #3
Hey Kevin,
I'm pretty sure the three are identical:

..method private instance void test() cil managed
{
// Code Size: 21 byte(s)
.maxstack 1
.locals (
[System.Web]System.Web.UI.WebControls.TextBox box1,
[System.Web]System.Web.UI.WebControls.TextBox box2,
[System.Web]System.Web.UI.WebControls.TextBox box3)
L_0000: nop
L_0001: newobj instance void
[System.Web]System.Web.UI.WebControls.TextBox::.ctor()
L_0006: stloc.0
L_0007: newobj instance void
[System.Web]System.Web.UI.WebControls.TextBox::.ctor()
L_000c: stloc.1
L_000d: newobj instance void
[System.Web]System.Web.UI.WebControls.TextBox::.ctor()
L_0012: stloc.2
L_0013: nop
L_0014: ret
}
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Numbers 1 and 3 are better in terms of performance (not sure if either of
them is better than the other). However, it is minimal.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

"Paul" <pa*******@smmoving.com> wrote in message
news:#1**************@TK2MSFTNGP11.phx.gbl...
With respect to performance, is either of these better?

Dim myTextBox As New TextBox

or

Dim myTextBox As TextBox
myTextBox = New TextBox

or

Dim myTextBox As TextBox = New TextBox


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by lastusernameleft | last post: by
1 post views Thread by Markus Rebbert | last post: by
9 posts views Thread by Robert Lario | last post: by
22 posts views Thread by smartwolf agassi via DotNetMonster.com | last post: by
33 posts views Thread by Protoman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.