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# / May 2008

Tip: Looking for answers? Try searching our database.

Early determination

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sin Jeong-hun - 27 May 2008 06:31 GMT
if( (myList!=null) && (myList.Count>10) )
{
    Do something....
}

The code above throws an exception if "myList" is null. Even though
the left condition is false so that the whole condition must be false,
it looks like C# also tries to evaluate the right condition. To avoid
the exception I have to code as

if( myList!=null )
{
    if( myList.Count>10 )
    {
         Do something....
    }
}

This can be trivial but,it requires a little more typing and rows in
the source code. I think C# doesn't need to check the right condition
in this situation, What do you think?
Jon Skeet [C# MVP] - 27 May 2008 06:58 GMT
> if( (myList!=null) && (myList.Count>10) )
> {
[quoted text clipped - 3 lines]
>
> The code above throws an exception if "myList" is null.

No, it doesn't. Are you sure you haven't got a single & instead of a
double one? C# is definitely designed to handle this situation.

Could you post a short but complete program demonstrating the problem?
See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon
kodehoved - 27 May 2008 07:47 GMT
> > if( (myList!=null) && (myList.Count>10) )
> > {
[quoted text clipped - 12 lines]
>
> Jon

I agree with Jon. Unless you have multiple threads modifying myList at
the same time C# will do as you expect, i.e. evaluate only as much of
the expression as necessary. Actually, in a release build the two
pieces of code compile to the exact same IL.

If you do in fact have multiple threads here, both solutions are
equally flawed as checking and accessing myList is not a single
operation in either of the cases. In that case, you need
synchronization.

Brian
Sin Jeong-hun - 27 May 2008 14:38 GMT
> > > if( (myList!=null) && (myList.Count>10) )
> > > {
[quoted text clipped - 24 lines]
>
> Brian

That was weird. I tested it but it caused no exception. Actually, that
was not exactly the same situation which I usually encounter while I'm
programming. I can't recreate the code that caused an exception right
now. When I it comes to me, I will post it again.

I'm sorry for having caused troubles by asking you a wrong question.
Thank you.

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.