Loop through collections

Posted by ScG on Stack Overflow See other posts from Stack Overflow or by ScG
Published on 2010-04-05T11:57:49Z Indexed on 2010/04/05 12:03 UTC
Read the original article Hit count: 180

Filed under:

I have two classes

class A
{
    public string something { get; set; }
    public IList<B> b= new List<B>();
}

class B
{
    public string else { get; set; }
    public string elseelse { get; set; }
}

I have populated an object of class A called obj. How can I loop through this object and print values. Do I have to use two foreach's like the one show here or is there a better way?

 foreach (var z  in obj)
            {
                // print z.something;
                foreach (var x in z.b)
                {
                    // print x.elseelse;
                }
            }

© Stack Overflow or respective owner

Related posts about c#