runtime stack

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chance Hopkins

    runtime stack

    Can anyone tell me in a few words, is one of the following better and if so
    which one and why:

    if(varone != null)
    if(varone != "")
    dosomething();

    if(varone != null && varone!= "")
    dosomething();

    I'm thinking what I need to learn more about is the "stack". Is that the
    correct term? Anyone have references?

    Thanks.



  • Joshua Flanagan

    #2
    Re: runtime stack

    Chance Hopkins wrote:[color=blue]
    > Can anyone tell me in a few words, is one of the following better and if so
    > which one and why:
    >
    > if(varone != null)
    > if(varone != "")
    > dosomething();
    >
    > if(varone != null && varone!= "")
    > dosomething();
    >
    > I'm thinking what I need to learn more about is the "stack". Is that the
    > correct term? Anyone have references?
    >
    > Thanks.
    >
    >
    >[/color]

    Comment

    • Joshua Flanagan

      #3
      Re: runtime stack

      There is no difference. They will both produce identical IL code.

      This isn't related to the stack. The stack would be relevant if you
      were concerned about the number of variables you are using within a method.

      If you want to see for yourself that they are exactly the same (and
      learn how to figure these things out), copy the following text to a file
      (test.cs):

      using System;
      public class StackTest {
      public void FirstTry(string varone){
      if(varone != null)
      if(varone != "")
      dosomething();
      }

      public void SecondTry(strin g varone){
      if(varone != null && varone!= "")
      dosomething();
      }

      public void dosomething(){
      Console.WriteLi ne("hello");
      }
      }

      Compile from the command line:
      csc /t:library /out:test.dll test.cs

      Load the library in the IL Disassembler:
      ildasm test.dll

      Double click on FirstTry. Double click on SecondTry. Compare the
      contents of the 2 windows that popped up. The instructions are the same.


      Joshua Flanagan

      Comment

      • Chance Hopkins

        #4
        Re: runtime stack

        Thanks, I will try this tomorrow when I am more awake.

        Do you know of any resources where I can read about the stack? I'd like to
        understand more about that too. I stumbled upon some comparison in a
        newsgroup post but can't find it again and don't know where to start.



        "Joshua Flanagan" <josh@msnews.co m> wrote in message
        news:u0khxX8RFH A.2664@TK2MSFTN GP15.phx.gbl...[color=blue]
        > There is no difference. They will both produce identical IL code.
        >
        > This isn't related to the stack. The stack would be relevant if you were
        > concerned about the number of variables you are using within a method.
        >
        > If you want to see for yourself that they are exactly the same (and learn
        > how to figure these things out), copy the following text to a file
        > (test.cs):
        >
        > using System;
        > public class StackTest {
        > public void FirstTry(string varone){
        > if(varone != null)
        > if(varone != "")
        > dosomething();
        > }
        >
        > public void SecondTry(strin g varone){
        > if(varone != null && varone!= "")
        > dosomething();
        > }
        >
        > public void dosomething(){
        > Console.WriteLi ne("hello");
        > }
        > }
        >
        > Compile from the command line:
        > csc /t:library /out:test.dll test.cs
        >
        > Load the library in the IL Disassembler:
        > ildasm test.dll
        >
        > Double click on FirstTry. Double click on SecondTry. Compare the
        > contents of the 2 windows that popped up. The instructions are the same.
        >
        >
        > Joshua Flanagan
        > http://flimflan.com/blog[/color]


        Comment

        • Gabriel Lozano-Morán

          #5
          Re: runtime stack

          if(varone != null)
          if(varone != "")
          dosomething();

          Will be translated to

          if ((varone != null) && (varone != ""))
          dosomething();

          So it doesn't really matter. But in the first approach you could still add
          an else statement if the condition varone equals "".

          Gabriel Lozano-Morán
          Software Engineer
          Sogeti

          "Chance Hopkins" <chance_hopkins @hotmail.com> wrote in message
          news:%23PaXJt7R FHA.3560@TK2MSF TNGP14.phx.gbl. ..[color=blue]
          > Can anyone tell me in a few words, is one of the following better and if
          > so which one and why:
          >
          > if(varone != null)
          > if(varone != "")
          > dosomething();
          >
          > if(varone != null && varone!= "")
          > dosomething();
          >
          > I'm thinking what I need to learn more about is the "stack". Is that the
          > correct term? Anyone have references?
          >
          > Thanks.
          >
          >
          >[/color]


          Comment

          • Joshua Flanagan

            #6
            Re: runtime stack

            What kind of information are you looking for? I cannot think of what
            you may have read that would have related the construction of
            conditional statements with the stack.

            Any general computer science text should be able to explain the concepts
            of stack allocation vs. heap allocation.
            A quick search turns up this link, which explains the basics:


            In .NET terms, you may have heard of "value types" (stored on the stack)
            vs. "reference types" (allocated on the heap).

            You can read about value types in the .NET Framework here:





            Chance Hopkins wrote:[color=blue]
            > Thanks, I will try this tomorrow when I am more awake.
            >
            > Do you know of any resources where I can read about the stack? I'd like to
            > understand more about that too. I stumbled upon some comparison in a
            > newsgroup post but can't find it again and don't know where to start.
            >
            >
            >
            > "Joshua Flanagan" <josh@msnews.co m> wrote in message
            > news:u0khxX8RFH A.2664@TK2MSFTN GP15.phx.gbl...
            >[color=green]
            >>There is no difference. They will both produce identical IL code.
            >>
            >>This isn't related to the stack. The stack would be relevant if you were
            >>concerned about the number of variables you are using within a method.
            >>
            >>If you want to see for yourself that they are exactly the same (and learn
            >>how to figure these things out), copy the following text to a file
            >>(test.cs):
            >>
            >>using System;
            >>public class StackTest {
            >> public void FirstTry(string varone){
            >> if(varone != null)
            >> if(varone != "")
            >> dosomething();
            >> }
            >>
            >> public void SecondTry(strin g varone){
            >> if(varone != null && varone!= "")
            >> dosomething();
            >> }
            >>
            >> public void dosomething(){
            >> Console.WriteLi ne("hello");
            >> }
            >>}
            >>
            >>Compile from the command line:
            >>csc /t:library /out:test.dll test.cs
            >>
            >>Load the library in the IL Disassembler:
            >>ildasm test.dll
            >>
            >>Double click on FirstTry. Double click on SecondTry. Compare the
            >>contents of the 2 windows that popped up. The instructions are the same.
            >>
            >>
            >>Joshua Flanagan
            >>http://flimflan.com/blog[/color]
            >
            >
            >[/color]

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: runtime stack

              Joshua Flanagan <josh@msnews.co m> wrote:

              <snip>
              [color=blue]
              > In .NET terms, you may have heard of "value types" (stored on the stack)
              > vs. "reference types" (allocated on the heap).[/color]

              Although you may have heard that, it's a commonly-stated but inaccurate
              description.

              See http://www.pobox.com/~skeet/csharp/memory.html

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              Working...