什么是自定义集合? [英] What is a custom collection?

查看:233
本文介绍了什么是自定义集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对象的集团。但是我有以下情况的混乱。

A Group of objects. However i am having confusion in the following case.

一个示例类

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.

推荐答案

描述的集合作为一个组对象在技术上是正确的,但也未免太还原。一个更好的问题是什么是有用的集合?,这里的有用带给你问自己我怎么需要使用它?

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"?

现在,在的a_list阵列领域绝对是一个集合(而不是自定义之一),而你的的a_list 是(可以说)的集合,但它是一个有用的?

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?

一个集合是一组的一般与的,你放在一起,以对象来概括对它们进行一些操作。你可能想的使用的集合的方式有很多,但最常见的已经pretty的许多编程语言很多标准化。 例如,为了对的集合,以通常需要至少能够读取其元件中只进循环从第一个到最后一个是有用的。 您的a_list不提供这种直接。您可以轻松地添加自定义的方法,这样做,但你可以往前走了一步,实现现有的接口。

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.

如果您实现 的IEnumerable&LT; T&GT; 上的a_list你不仅能够遍历它,但也可以使用自己的a_list互换与其他集合了很多现有的code希望获得的IEnumerable 。例如:

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();
    }
}

将允许你这样做你的

foreach(var a in just_an_object)
{
    Console.WriteLine(a.Foo);
}

或者你可以使用LINQ使用它:

Or you could use it with Linq:

var numbered=just_an_object.Zip(Enumerable.Range(0,3),(a,b) => b.ToString() + a.Foo);

现在编号为的IEnumerable&LT;字符串&GT; 有三个值(0some,1another,-2-酮),但这是不重要的;最重要的一点是,(通过实施的IEnumerable&LT; T&GT; )just_an_object可以与LINQ的方法,它以同样的方式适用于数组或列表或其他大部分用于标准的集合。 而且,请注意,无论是的foreach 邮编知道需要你的列表字段是阵列,而不是名单,其中,A&GT; 红黑树你实现自己,所以你可能要使它私人

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.

有其他的事情,除了通过它迭代,你可能想要做一个集合。添加或删除元素,提供了一个索引(这样就可以访问第n个元素与 just_an_object [N] )。与IEnumerable的,也有接口,使您可以实现他们在一个标准化的方式(寻找的ICollection和IList的)。

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...

这篇关于什么是自定义集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆