Resharper thinks that typecast is redundant, but w

2019-05-12 18:16发布

问题:

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 ?

回答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.