Resharper thinks that typecast is redundant, but without typecast the code does not work

时光毁灭记忆、已成空白 提交于 2019-12-22 09:57:10

问题


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.



来源:https://stackoverflow.com/questions/33258145/resharper-thinks-that-typecast-is-redundant-but-without-typecast-the-code-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!