Why can't I access the class internals of List<T> when I derive from it?
class NewList<T> : List<T>
Why can't I access it's internals like T[] _items
, etc?
Why aren't they protected, but private?
Should I use composition for this?
They're private because the author (Microsoft) did not intend for you to access them, probably for safety reasons.
You should use composition instead of inheritance. Make your class expose IList<T>
instead of List<T>
directly.
List<T>
should never be directly exposed in a public API (this is actually specified in the design guidelines). It should be used as an internal implementation detail only.
List<T>
isn't designed to be a base class. You should use one of the classes in System.Collections.ObjectModel instead.
Why can't I access it's internals like T[] _items, etc?
Reflector says _items is private. The underscore suggests a private member
Why aren't they protected, but private?
In the case of _items, Microsoft provides the public ToArray method. It returns _items.
链接地址: http://www.djcxy.com/p/53878.html上一篇: 过滤Java集合的最佳方法是什么?