runtime stack

  • Thread starter Thread starter Chance Hopkins
  • Start date Start date
C

Chance Hopkins

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.
 
Chance said:
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.
 
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(string varone){
if(varone != null && varone!= "")
dosomething();
}

public void dosomething(){
Console.WriteLine("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
 
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.
 
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
 
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:
http://www-ee.eng.hawaii.edu/Courses/EE150/Book/chap14/subsection2.1.1.8.html

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:
http://msdn.microsoft.com/library/d...genref/html/cpconvaluetypeusageguidelines.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconValueTypes.asp
 
Back
Top