.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

C#: Shortcutting behavior

C# Newbie here,

Given the following:

if ((MyString.Length >= 3) & (MyString.Substring(0,3) == "HEL"))

apps crash with an ArgumentOutOfRange exception if MyString is less than three characters long.

In other languages I've used, shortcutting would evaluate the left side, determine that MyString.Length < 3, and never evaluate the right side.

Is there any way to duplicate this behavior in C#?
Karl Perry Send private email
Thursday, November 20, 2008
 
 
Try...

if ((MyString.Length >= 3) && (MyString.Substring(0, 3) == "HEL"))
N
Thursday, November 20, 2008
 
 
& is the bitwise and operator.
&& is the boolean and operator.

They two two very different things. The second one is the one you want and shortcuts as you expect.
Chris Tavares Send private email
Thursday, November 20, 2008
 
 
Ah.  Thanks!  I looked for "shortcut" in Help but only got keyboard shortcut ideas.

I never thought that "short-circuit" may be another term, and it doesn't exist in the C# Help index anyway.
Karl Perry Send private email
Thursday, November 20, 2008
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz