问题
A Group of objects. However i am having confusion in the following case.
A sample class
class A
{
public string Foo{get; set;}
}
class A_list
{
public A[] list;
public A_list(A[] _list)
{
list = new A[_list.Length];
for (int i = 0; i < _list.Length; i++)
{
list[i] = _list[i];
}
}
}
static void Main(String[] args)
{
A[] names = new A[3]
{
new A{Foo="some"},
new A{Foo="another"},
new A{Foo="one"}
};
A_list just_an_object = new A_list(names);
}
Which of the above is a custom collection the array or the object that holds array as a field or are both custom collections.
回答1:
Describing a collection as a "group of objects" is technically right but also a bit too reductive. A better question would be "what's a useful collection?", where the "useful" brings you to asking yourself "how do I need to use it"?
Now, the Array field in A_List is definitely a collection (but not a "custom" one), while your A_List
is (arguably) a collection, but is it a useful one?
A collection is a group of generally related objects that you put together in order to generalize some operation on all of them. The ways you may want to use the collection are many, but the most common ones have been pretty much standardized in many programming languages. For instance, in order for a collection to be "useful" you generally need at least to be able to read its elements in a forward-only loop from the first to the last. Your A_List does not provide this directly. You could easily add custom methods to do so, but you could go a step ahead and implement an existing interface.
If you implemented IEnumerable<T> on A_List you would not only be able to iterate through it, but could also use your A_List interchangeably with other collections in a lot of existing code that expects a IEnumerable
. For example:
class A_list : IEnumerable<A>
{
// ... your other code
public IEnumerator<A> GetEnumerator()
{
return ((IEnumerable<A>)list).GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return list.GetEnumerator();
}
}
would allow you to do this in your main
:
foreach(var a in just_an_object)
{
Console.WriteLine(a.Foo);
}
Or you could use it with Linq:
var numbered=just_an_object.Zip(Enumerable.Range(0,3),(a,b) => b.ToString() + a.Foo);
Now numbered is a IEnumerable<String>
with three values (0some, 1another, 2one), but this is unimportant; the important bit is that (by implementing IEnumerable<T>
) just_an_object could be used with a Linq method that works in the same way with an Array or a List or most other "standard" collections.
And also, note that neither foreach
nor Zip
needed to know that your list
field is an Array
and not a List<A>
or a red-black tree you implemented yourself, so you may want to make it private
.
There are other things,besides iterating through it, that you may want to do with a collection. Adding or removing elements, providing a indexer (so that you can access the n-th element with just_an_object[n]
). As with IEnumerable, there are interfaces that enable you to implement them in a "standardized" way (look for ICollection and IList).
And then there are the "custom" aspects. The ones that made you decide that you needed your custom collection instead of directly using Array or List that already do all these things for you out-of-the-box...
来源:https://stackoverflow.com/questions/13903426/what-is-a-custom-collection