Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Evaluation order question ...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jamie Risk - 17 Nov 2006 16:00 GMT
Is this valid?

  byte ptr[];
  ...
  if (null != ptr && ptr.Length > 0) {
     ...
  }

(It would be in C)
Or should I break it apart:

  if (null != ptr) {
     if (ptr.Length > 0) {
        ...
     }
  }
Tom Porterfield - 17 Nov 2006 16:04 GMT
> Is this valid?
>
[quoted text clipped - 3 lines]
>      ...
>   }

This is fine as the expression is evaluated left to right in C# and the if
will be exited when the first condition is found that evaluates to false.
So if your ptr is null, the ptr.Length code will never execute.

> (It would be in C)
> Or should I break it apart:
[quoted text clipped - 4 lines]
>      }
>   }

You can do it this way if you like, but it isn't necessary.
Signature

Tom Porterfield

Dave Sexton - 17 Nov 2006 16:07 GMT
Hi Jamie,

They do the same thing, but I prefer the first one because it fits nicely on
one line.  BTW, it's more common in C# to write it as follows:

if (ptr != null && ptr.Length > 0)
{
   ...
}

The compiler prevents accidental assignment to ptr because it requires the
expression to evaluate to a boolean, not byte[].

Signature

Dave Sexton

> Is this valid?
>
[quoted text clipped - 12 lines]
>      }
>   }
Bryan - 17 Nov 2006 16:07 GMT
Yes, C# will short-circuit that if the first condition is false.  That is
part of the C# spec and every compiler should honor that.  I believe in the
C/C++ world, that sort of thing is up to the compiler vendor.  In C# you can
count on it being there (unless you have a nonstandard compiler vendor).

A side note: you should write ptr != null instead of the other way around.  
It reads better to someone looking at your code.  All the reasons you did it
the other way around in C are no longer valid in C#.

> Is this valid?
>
[quoted text clipped - 12 lines]
>       }
>    }
Mark Wilden - 17 Nov 2006 18:03 GMT
> Yes, C# will short-circuit that if the first condition is false.  That is
> part of the C# spec and every compiler should honor that.  I believe in
> the
> C/C++ world, that sort of thing is up to the compiler vendor.

Short-circuit evaluation is also required by the language definition in C
and C++ and always has been. It was just another delight for me when moving
from BASIC and Pascal.

///ark

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.