Can the Jitter/runtime resolve down one level when calling foreach on an interface where a strongly typed enumerator exists? Or otherwise prevent boxing; can always just be for the foreach case.
There may need to be a pattern; for example the Interface enumerator returning result of call to strongly typed Enumerator.
Enumerating Dictionary doesn't allocate; however casting to an IDictionary then enumerating allocates. Shouldn't it follow the same code path as Dictionary?
var dict = new Dictionary<string, string>();
var idict = (IDictionary<string, string>)dict;
for (var i = 0; i < 1000000; i++)
{
// Doesn't allocate
foreach (var item in dict)
{
;
}
}
for (var i = 0; i < 1000000; i++)
{
// Allocates 998k
foreach (var item in idict)
{
;
}
}

Can the Jitter/runtime resolve down one level when calling
foreachon an interface where a strongly typed enumerator exists? Or otherwise prevent boxing; can always just be for theforeachcase.There may need to be a pattern; for example the Interface enumerator returning result of call to strongly typed Enumerator.
Enumerating
Dictionarydoesn't allocate; however casting to anIDictionarythen enumerating allocates. Shouldn't it follow the same code path asDictionary?