Neat Things

foreach

Like Visual Basic .NET and Perl, C# provides a foreach statement, which can be used to enumerate over objects that implement IEnumerable.

Design Choice 1

When writing containers that store value types, using the IEnumerable design pattern has an unfortunate side effect. Because IEnumerable.GetEnumerator returns an IEnumerator and IEnumerator.Current is of type object, the only way to return values from an enumerator is as an object. For value types, this means that the Current property boxes the value type and then returns it to the client, which immediately unboxes it. This boxing and unboxing is unfortunate from a performance standpoint.

To get around this problem, C# supports an alternative way of doing enumerators. If ...

Get Programming in the .NET Environment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.