Why to use ICollection<T> instead of List<T> and if i use ICollection<T>, then how can i use AddRange,Insert and other list methods [duplicate]

StackOverflow https://stackoverflow.com/questions/16728857

Domanda

I am using FxCop tool for code analysis and it shows an critical errors like "Don't expose generice list" and it suggests that instead of using a list object try to use ICollection.

I tried to replace the List with ICollection but so many places i have insert and Add range methods, i need them anyhow for fulfilling my business logic

"So How I can use Insert,AddRange and other list methods on Icollection without type casting or using ToList() method because if i had to use these methods, then why i should replace the list wiht ICollection"

Please don't mark anything redundantly duplicate if you don't understand what is my issue.

È stato utile?

Soluzione

exposing List is not a good practice because you can modify original collection with methods like Add, is better to expose IEnumerable this interface will not allow to change collection, the my advantage of this you will keep logic with managing your collection in one place

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top