Pagico is perfect for anyone who has to juggle many different tasks in their business or personal life. Sidenote: If you do purchase Pagico, I’d be grateful if you supported me by clicking on this link to purchase. It will save you a lot of time, money, and energy (not to mention the stress and hassle) by allowing you to plan ahead so you don’t have to waste countless hours getting bogged down with distractions. In fact, this type of software can be the difference between your success and failure, particularly when you are wearing many different hats as a business owner. In short, this is a capable tool that is priced affordably to help individuals and teams get more organized. (It's fine, and even possibly beneficial, to expose that list in other contexts using only ICollection, but that's an entirely different discussion.)Īs long as I'm answering, I will mention that I am skeptical that you've correctly understood the intent of the FxCop warning, as exposing generic lists is not in and of itself inherently harmful.This professional-grade project management tool does the work of multiple apps to transform your lists and tasks into clear and meaningful timelines so you can plan ahead easily and stay on track with all your work. ![]() Just leave the reference as List, at least in any context where you actually need to modify the collection. You can, since you are using the generic ICollection interface, write your own AddRange() as an extension method: public static void AddRange(this ICollection collection, IEnumerable range)īut I'm not convinced this is really all that much better than just leaving the type as List, if the goal is to be able to modify the object and use an AddRange() method.Ĭasting an ICollection reference back to its underlying List (as another answerer proposes) is pointless, as doing so would negate any value in using the ICollection interface in the first place. In the example you give, there is not really a much better solution than simply not hiding the List in the first place. In a sense, your question is the List equivalent to the very common question "why isn't string.Replace() working?" Since the object you're calling AddRange() with is not in fact the original collection, the original collection remains unchanged. The problem is, you didn't read the documentation for the ToList() extension method closely, and so don't realize that the ToList() method returns an entirely new object. So please help me with the understanding. But couldn't find a reason that satisfied me. When I tried to cast to parent reference ( ICollection) to child class object ( List), why doesn't Intellisense suggest AddRange(). ![]() List inherits from ICollection and List has AddRange(), etc functions. Gc.ToList().AddRange(sampleList) // sampleList items are not getting added to gc. ![]() New GenericClass ,Ĭonsole.WriteLine(gc.GetType()) // gc is of type Why it is not adding the items to the collection.īelow is the code: public class GenericClass ![]() Here is the sample code gc.ToList().AddRange(sampleList) It is not adding the collection of elements to the collection object. But later point of time, while doing the unit testing I found that the AddRange() is not working properly as expected. So I tried changing the List to ICollection. Recently I got a task to work on one of the FxCop warning - Do not expose generic lists.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |