After running this code:
var input = new List<T>( ... );
var result = input.Select( t => new U(t) );
U first1 = null;
foreach ( U u1 in result )
if ( first1 == null )
first1 = u1;
U first2 = null;
foreach ( U u2 in result )
if ( first2 == null )
first2 = u2;
Then 'first1 == first2' evaluates to false even though both U's wrap the same T. I haven't tested it yet, but I think it can be made to evaluate to true by chaining a .ToList() or .ToArray() onto the Select() call.
In real code, which is much more complex than this simple illustration, what is a good rule of thumb to use for deciding if .ToList() or .ToArray() should be appended? My initial thoughts are either any referenced expression that may be iterated more than once or, to be safer in case potential iterations are not obvious, any referenced expression whose result will never change.
first1 == first2
is false? Iterating on the same collection should return the same objects every time. – TenU
uses standard reference equality. Each iteration causes the sequence to be re-evaluated, so you get newU
instances. – Mclaurinnew U
every time you iterate over the collection? Or that newU(t) != new U(t)
? Lazyness has consequences. Whether those consequences are desirable or not depends on the use case. – Bigelow