3

Iterating JsonData through foreach fetches IDictionary enumerator instead of IList one.

foreach (var jsonEntry in jsonData)

This causes my code to throw an error.

InvalidOperationException: Instance of JsonData is not a dictionary
LitJson.JsonData.EnsureDictionary ()
LitJson.JsonData.System.Collections.Specialized.IOrderedDictionary.GetEnumerator ()
LitJson.JsonData.System.Collections.IDictionary.GetEnumerator ()

Casting the object to IList causes Resharper to issue a warning "Type cast is redundant."

foreach (var jsonEntry in jsonData as IList)

Why does Resharper think the cast is redundant ?

Schullz
  • 283
  • 1
  • 6
  • 18
jellyfication
  • 1,595
  • 1
  • 16
  • 37

1 Answers1

1

Why does Resharper think the cast is redundant ?

If you think this way:

IList is a ICollection and IDictionary is also a ICollection because both of them implement 'ICollection', Resharper's warning should become clear.

GrayFox
  • 997
  • 1
  • 9
  • 26
  • 1
    Unfortunately it doesn't seem to see that casting to `IList` forces `foreach` to call another `GetEnumerator()` implementation. Can't really blame JetBrains because it looks like a very rare case. – AndreySarafanov Oct 21 '15 at 11:44