if eval1 || eval2

  • Thread starter Thread starter Peter Hartlén
  • Start date Start date
P

Peter Hartlén

Hi!

We are using C# and VS2005 with a project migrated from CF1 and VS2003. When
running if-statements with two evaluations, where the second depends on the
first evaluation, we get runtime exceptions after our migration to CF2.

if a || b
{
}

If a is true, b shoulnd't be evaluated, right?

Example:
string line;
if( line == string.Empty || line.SubString(0,1) == "[" )

(alternatively: if( (line == string.Empty) || (line.SubString(0,1) ==
"[") ) )

Using this we get runtime errors...
 
I would write this as:

if ((line == null) || (line.Length == 0) || (line[0] == '['))

Check everything and doesn't create another string.

Hilton
 
Hilton is right. Try not to create new strings from strings which may will
be null or empty. The CF JIT isn't fine-grained enough to work at the
individual expression level, rather it will try to evaluate the entire
statement. In this scenario, if an expression evaluates to null and the JIT
tries to access its value, you will get an exception.

Keep in mind that strings are immutable in .NET and that the expression,
line.SubString(0,1) == "[", will actually create 2 strings on the heap
everytime it is called before it gets evaluated.

--
-------------------------
Neil Cowburn
Principal Partner
OpenNETCF Consulting, LLC


Hilton said:
I would write this as:

if ((line == null) || (line.Length == 0) || (line[0] == '['))

Check everything and doesn't create another string.

Hilton




Peter Hartlén said:
Hi!

We are using C# and VS2005 with a project migrated from CF1 and VS2003.
When running if-statements with two evaluations, where the second depends
on the first evaluation, we get runtime exceptions after our migration to
CF2.

if a || b
{
}

If a is true, b shoulnd't be evaluated, right?

Example:
string line;
if( line == string.Empty || line.SubString(0,1) == "[" )

(alternatively: if( (line == string.Empty) || (line.SubString(0,1) ==
"[") ) )

Using this we get runtime errors...
 
Thanks.



Neil Cowburn said:
Hilton is right. Try not to create new strings from strings which may will
be null or empty. The CF JIT isn't fine-grained enough to work at the
individual expression level, rather it will try to evaluate the entire
statement. In this scenario, if an expression evaluates to null and the
JIT tries to access its value, you will get an exception.

Keep in mind that strings are immutable in .NET and that the expression,
line.SubString(0,1) == "[", will actually create 2 strings on the heap
everytime it is called before it gets evaluated.

--
-------------------------
Neil Cowburn
Principal Partner
OpenNETCF Consulting, LLC


Hilton said:
I would write this as:

if ((line == null) || (line.Length == 0) || (line[0] == '['))

Check everything and doesn't create another string.

Hilton




Peter Hartlén said:
Hi!

We are using C# and VS2005 with a project migrated from CF1 and VS2003.
When running if-statements with two evaluations, where the second
depends on the first evaluation, we get runtime exceptions after our
migration to CF2.

if a || b
{
}

If a is true, b shoulnd't be evaluated, right?

Example:
string line;
if( line == string.Empty || line.SubString(0,1) == "[" )

(alternatively: if( (line == string.Empty) || (line.SubString(0,1) ==
"[") ) )

Using this we get runtime errors...
 
Back
Top