Forgive me if this is a duplicate, but it’s a minor issue for me and I can only spend so long on my curiosity. Why is it that when I use an implicitly typed loop variable in a foreach block, I get no Intellisense? The inferred type seems to be quite obvious.
I am using ReSharper, but when I switch the Intellisense to VS I get the same behaviour, and this don’t think it’s to blame.
EDIT: Sorry, a bit later, but I was iterating DataTable.Rows, which uses an untyped ieterator, as Marc explains below.
I suspect that the data you are enumerating is not typed – for example, a lot of things that were written in 1.1 only implement
IEnumerable, and don’t have a custom iterator (you don’t actually needIEnumerable<T>to do typed iteration – and indeed you don’t even needIEnumerableto useforeach; a lot of 1.1 typed wrote special enumerator types to avoid boxing/casting etc – lots of work). In many cases it would be a breaking change to fix them.A trivial example here is
PropertyDescriptorCollection:but actually,
PropertDescriptorCollection‘s enumerator is justIEnumerator, soCurrentisobject– and hence you’ll always getobjectwhen you usevar:Contrast this to the (equally 1.1)
StringCollection; this has a custom enumerator (StringEnumerator); so if you usedforeachwithvar, you’d getstring(notobject).In anything 2.0 and above, it would be reasonable to expect better typing, for two reasons:
But even then there are still cases when you don’t get the type you expect; you can either (and perhaps more clearly) specify the type manually, or use
Cast<T>()/OfType<T>().