C# Downcasting Lists to IList "special tactics" -



C# Downcasting Lists to IList "special tactics" -

so i'm working @ company , i'm relatively new here i've come across unusual in production code i'm told written much more knowledgable in c# myself "does things special way". i'm trying think of reasons i'm explain, giving benefit of uncertainty can't come anything.

ilist<facility> facilities = new list<facility>();

the above line @ core of problem... other oddities throughout code come beingness unable utilize interfaces things , hence info has copied out list before using them (for illustration exporting via webservice, not allow using interface, must implementation of ilist)

now understand interfaces wonderful in sense can write methods expect methods presribed in interface exist on implementation beingness passed method but...

can please help me out here... can think of reason you'd downcast list interface in info layer of application?

also create clear... these lists not used interchangeable info types either, occurs @ point list defined , stored, , there ilist per datatype stored.

thanks!

it considered practice work highest possible class in hierarchy. if need loop through values should expose ienumerable<t>:

ienumerable<facility> facilities = new list<facility>(); // can foreach on facilities or chain linq

if need indexer access utilize ilist<facility>

ilist<facility> facilities = new list<facility>(); // in add-on looping can access facilities index // , can add together , remove facilities

the thought behind exposing highest possible class/interface in hierarchy when designing api way hiding implementation details using list<t>. if tomorrow invent hyperspeciallistwhichisveryfast<t> implements contract agreed upon consumer of api, consumer doesn't need alter in code. doesn't care. works ienumerable<t> because needs loop example. , new list code run faster without him needing alter anything.

but using non-generic à la .net 1.0 weakly typed collections in history books voodoo magic:

ilist facilities = new list<facility>();

cannot think of reason why useful. downcasting non-generic version forcefulness client cast in order access underlying type t. way loosing compile-time safety generics provide.

c#

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -