c# - When to use List<dynamic> instead of List<T> -


in code review have found interesting construction:

public actionresult seatmap(string name) {    var equimpment = this.customresourcescache.getallequipment(name);     if (string.isnullorempty(name))    {       list<dynamic> = new list<dynamic>();       foreach (var item in equimpment)       {          all.add(item.tag[0]);       }       return cachedresult(new { seatmaps = } );      }      return cachedresult(new { seatmaps = equimpment.where(o => o.key == name).count()>0?equimpment.where(o => o.key == name).first().tag:new list<dynamic>() });    } 

i wondering why used list <<[dynamic]>> instead of list<<[t]>> , there other solution instead of one.

thanks

here's 1 rare scenario in find code acceptable.

let's getallequipment returns list of equipment. equipment not contain key property code wants, serves base class various equipment classes, of have key property, don't share interface. in case, there no t can put in list<t>.

now, if own code equipment , children, solution create shared interface key inside (and use interface t).

but if don't own code, if it's third party library, using list<dynamic> seems acceptable, understanding future changes library might break code (i.e if introduce new child equipment doesn't have key property).


Comments