Which is better?

Discussion in 'ASP .Net' started by Paul, Nov 24, 2004.

  1. Paul

    Paul Guest

    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
    Paul, Nov 24, 2004
    #1
    1. Advertising

  2. Paul

    Patrice Guest

    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" <> a écrit dans le message de
    news:%231K5d$...
    > 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
    >
    >
    Patrice, Nov 24, 2004
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:#1K5d$...
    > 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
    >
    >
    Kevin Spencer, Nov 24, 2004
    #3
  4. Paul

    Karl Seguin Guest

    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" <> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:#1K5d$...
    > > 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
    > >
    > >

    >
    >
    Karl Seguin, Nov 24, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dot_net_junky

    Which is better, C# or C++?

    dot_net_junky, Feb 9, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    433
    Tor Bådshaug
    Feb 14, 2005
  2. Replies:
    0
    Views:
    379
  3. Peter Bencsik
    Replies:
    2
    Views:
    808
  4. Andrew Thompson
    Replies:
    8
    Views:
    132
    Premshree Pillai
    Jun 7, 2005
  5. Replies:
    2
    Views:
    42
    Mark H Harris
    May 13, 2014
Loading...

Share This Page