views:

91

answers:

1

Hi!

I deal with a framework on a daily basis where we sometimes provide methods that accept IEnumerable<MyBusinessObject> as a parameter in order to show user interfaces, perform calculations etc.

If I pass in an array of MyBusinessObject like so:

MyBusinessObject[] myArray = new MyBusinessObject { obj1, obj2, ..., objN };
frameworkClass.MyMethod(myArray);

....

public class FrameworkClass
{
    public void MyMethod(IEnumerable<MyBusinessObject> objs)
    {
        // Other code that uses the enumerable
        MyBusinessObject[] objectArray = objs.ToArray();            
        // More code that uses the enumerable
    }
}

Does the line objs.ToArray() simply resolve the IEnumerable<MyBusinessObject> back to the original array, or does it copy it to a whole new array, ready for use?

+10  A: 

No, you will always get a new copy of the array, though the objects in it aren't copies, they are the same references as in the original array.

It would be very inconsistent for changes to the returned array to sometimes affect the source and sometimes not. ToList works the same way for the same reason.

Neil Barnwell
Thanks for the quick answer - you got the "tick" because you were quicker on the draw than Jon Skeet. Thanks!
Codesleuth
Being quicker than Jon is always worth an upvote...!
Pontus Gagge
Not that Jon needs the rep anyway...
David Rodríguez - dribeas
@Codesleuth: Just out of interest, in what way was Neil quicker than me? I'm not asking for the tick or anything, but by my reckoning I was 4 minutes earlier than Neil... (and in particular, my answer ID is lower ;)
Jon Skeet
I was totally beaten by Jon, I just wanted to add a tiny extra bit of detail, or else I wouldn't even have posted the answer. I feel bad now. :(
Neil Barnwell
@Neil: No need to feel bad at all. Both our answers include extra bits. In fact, it might be best if one of us edits your answer to include the bit in my answer about consistency and it being the same for ToList - then I can delete my answer.
Jon Skeet
@Jon Skeet: Ok, it seems I didn't quite read the timestamps properly. When I looked, it said Neil was "4 minutes ago" and you were "6 minutes ago". I've messed up. Would you like me to tick you as answer? @Neil: would you like to concede to Jon's answer? lol
Codesleuth
@Codesleuth: No, let's leave it. I'll amalgamate the answers though, to have all the useful information in one place.
Jon Skeet
Very kind of you, Jon. Few rep points never hurt anyone but the main thing is that the right answer is there.
Neil Barnwell
@Neil: Of course I don't lose any rep by deleting my answer in this case :)
Jon Skeet
I'm not sure if this should be another question, but I just wanted to quickly see if you know this too: does `IEnumerable<T>.Count()` call the array's `.Length` property to get a result, or does it do something horribly inefficient?
Codesleuth
Have to bow to Mr Skeet for that one. Personally, I bet it doesn't (IEnumerable doesn't have a Length property). However there's a chance it does some clever reflection stuff for certain scenarios as an optimisation. Point is, if you want a count, IEnumerable isn't appropriate - IList<T> is.
Neil Barnwell