Jamie Dixon Jamie Dixon - 1 year ago 116
C# Question

What happens when casting IEnumerable to ICollection

Given a case such that:

var collection = myEnumerable as ICollection<MyType>;

What happens under the hood?
has a count property. Does this casting enumerate the enumerable to get the count or does something more involved happen?

Answer Source

Nothing happens. If myEnumerable indeed is an ICollection<MyType> collection will contain it. Otherwise it will be null. Simple as that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download