473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

foreach null

Hi

if I have a foreach loop over a list, do I get a NullReferenceException if
one of the objects in the list is null?

For example, say I have a list "persons" of IPerson objects, but one of the
objects in the list is actually null.

foreach (IPerson person in persons)
{
// do something with person...
}
Thanks,
Peter
Jun 1 '06 #1
4 12488
yes
Peter Kirk wrote:
Hi

if I have a foreach loop over a list, do I get a NullReferenceException if
one of the objects in the list is null?

For example, say I have a list "persons" of IPerson objects, but one of the
objects in the list is actually null.

foreach (IPerson person in persons)
{
// do something with person...
}
Thanks,
Peter


Jun 1 '06 #2
null is a perfectly valid IPerson, so no, foreach won't break; however, it
will be returned (as null), and so /your/ code might blow up when it tries
to use it; as such, you may wish to simply exclude this from your
considerations:

foreach(IPerson person in persons) {
if(person == null) continue;
// do something with person
}

However, if persons is null, then I would expect bad things to happen.

Marc
Jun 1 '06 #3

"Marc Gravell" <ma**********@gmail.com> skrev i en meddelelse
news:e7**************@TK2MSFTNGP02.phx.gbl...
null is a perfectly valid IPerson, so no, foreach won't break; however, it
will be returned (as null), and so /your/ code might blow up when it tries
to use it; as such, you may wish to simply exclude this from your
considerations:

foreach(IPerson person in persons) {
if(person == null) continue;
// do something with person
}

However, if persons is null, then I would expect bad things to happen.


Yes, thanks. I found the exception I was getting was actually from the
list - which came from another containing object, and was null.

Peter
Jun 1 '06 #4
Mike Barakat <ba*****@gmail.com> wrote:
yes


No. You end up with a null reference as the value of the variable
involved in the loop. Here's an example:

using System;
using System.Collections;

class Test
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add("Before null");
list.Add(null);
list.Add("After null");

foreach (string x in list)
{
Console.WriteLine ("Is the value null? {0}", x==null);
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: William Stacey | last post by:
Can you use a foreach to set the elements of an object to null? This does not work as o is read-only. Is only way to use a for loop? object buffer = new object; //...fill buffer. foreach...
9
by: Lloyd Dupont | last post by:
Does any of you have the slightest ideas of why you can't modify an array list while in a foreach of its element. I wrote my own collection and I'm trying to add the same behavior but I don't...
5
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) {...
16
by: Islam Elkhayat | last post by:
In my web application i use satalite assembly to localize my web form.. Instead of set the each control text to the resourcemanager getstring() method i try to use foreach loop private string...
5
by: james | last post by:
Hey Guys, Would anyone mind explaining why a foreach will implicitly do unsafe casts, and if there is a way to turn this off? Here is an example: ulong vals = new ulong { ulong.MaxValue };...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
22
by: J. Frank Parnell | last post by:
Hello, So, I was wondering how to do this: foreach($foo as $k=>$v AND $bar as $k2=>$v2){ echo '<TR><TD>$k</TD><TD>$v</TD><TD>$k2</TD><TD>$v2</TD></TR>; } Thanks,
4
by: Peter Morris | last post by:
I am not sure what you are asking. You seem to be asking how to implement a plain IEnumerable on a composite structure, but then your example shows a flat structure using "yield". Your subject...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.