名单< BusinessObject的>或BusinessObjectCollection? [英] List<BusinessObject> or BusinessObjectCollection?

查看:148
本文介绍了名单< BusinessObject的>或BusinessObjectCollection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此之前,C#泛型,每个人都将code集合为他们的业务对象通过创建一个实现IEnumerable的集合基

Prior to C# generics, everyone would code collections for their business objects by creating a collection base that implemented IEnumerable

IE浏览器:

public class CollectionBase : IEnumerable

,然后会从中导出他们的业务对象的集合。

and then would derive their Business Object collections from that.

public class BusinessObjectCollection : CollectionBase

现在与泛型列表类的,没有人只使用呢?我发现,我用的是两种技术的一种妥协:

Now with the generic list class, does anyone just use that instead? I've found that I use a compromise of the two techniques:

public class BusinessObjectCollection : List<BusinessObject>

我只是路过身边名表,是因为我喜欢有强类型的名称来代替。

I do this because I like to have strongly typed names instead of just passing Lists around.

什么是办法?

推荐答案

我一般在只使用一个列表直接,除非出于某种原因,我需要封装的数据结构,并提供了其功能的有限子集的阵营。这主要是因为,如果我没有进行封装特定的需要那么做,就只是在浪费时间。

I am generally in the camp of just using a List directly, unless for some reason I need to encapsulate the data structure and provide a limited subset of its functionality. This is mainly because if I don't have a specific need for encapsulation then doing it is just a waste of time.

不过,与集料初始化功能在C#3.0中,出现了一些新情况,我会主张使用自定义的集合类。

However, with the aggregate initializes feature in C# 3.0, there are some new situations where I would advocate using customized collection classes.

基本上,C#3.0允许任何类实现的IEnumerable 并有一个Add方法来使用新的聚合初始值的语法。例如,因为字典定义了一个方法,添加(K键,V值),可以使用这个语法来初始化词典:

Basically, C# 3.0 allows any class that implements IEnumerable and has an Add method to use the new aggregate initializer syntax. For example, because Dictionary defines a method Add(K key, V value) it is possible to initialize a dictionary using this syntax:

var d = new Dictionary<string, int>
{
    {"hello", 0},
    {"the answer to life the universe and everything is:", 42}
};

有关功能的最大好处是,它的add方法与任意数量的参数。例如,考虑到本系列:

The great thing about the feature is that it works for add methods with any number of arguments. For example, given this collection:

class c1 : IEnumerable
{
    void Add(int x1, int x2, int x3)
    {
        //...
    }

    //...
}

这将有可能将其初始化像这样:

it would be possible to initialize it like so:

var x = new c1
{
    {1,2,3},
    {4,5,6}
}

如果你需要创建复杂对象的静态表这可能是非常有用的。例如,如果你只是使用名单,其中,客户&GT; 和你想创造客户的静态列表对象,你必须创建它像这样:

This can be really useful if you need to create static tables of complex objects. For example, if you were just using List<Customer> and you wanted to create a static list of customer objects you would have to create it like so:

var x = new List<Customer>
{
    new Customer("Scott Wisniewski", "555-555-5555", "Seattle", "WA"),
    new Customer("John Doe", "555-555-1234", "Los Angeles", "CA"),
    new Customer("Michael Scott", "555-555-8769", "Scranton PA"),
    new Customer("Ali G", "", "Staines", "UK")
}

不过,如果您使用的是定制的集合,像这样的:

However, if you use a customized collection, like this one:

class CustomerList  : List<Customer>
{
    public void Add(string name, string phoneNumber, string city, string stateOrCountry)
    {
        Add(new Customer(name, phoneNumber, city, stateOrCounter));
    }
}

使用此语法然后,您可以初始化集合:

You could then initialize the collection using this syntax:

var customers = new CustomerList
{
    {"Scott Wisniewski", "555-555-5555", "Seattle", "WA"},
    {"John Doe", "555-555-1234", "Los Angeles", "CA"},
    {"Michael Scott", "555-555-8769", "Scranton PA"},
    {"Ali G", "", "Staines", "UK"}
}

这具有可既容易输入,并更容易阅读,因为它们是没有必要重新键入元素类型名称为每个元件的优点。其优点是特别强,如果元素类型为长或复杂。

This has the advantage of being both easier to type and easier to read because their is no need to retype the element type name for each element. The advantage can be particularly strong if the element type is long or complex.

话虽这么说,这是只有当你需要在你的应用程序定义的数据的静态收集有用的。某些类型的应用程序,如编译器,使用他们所有的时间。其他,像典型的数据库应用程序不要因为他们从数据库中加载的所有数据。

That being said, this is only useful if you need static collections of data defined in your app. Some types of apps, like compilers, use them all the time. Others, like typical database apps don't because they load all their data from a database.

我的建议是,如果你要么需要定义一个静态对象集合,或需要封装走采集接口,然后创建一个自定义集合类。否则,我只想用名单,其中,T&GT; 直接

My advice would be that if you either need to define a static collection of objects, or need to encapsulate away the collection interface, then create a custom collection class. Otherwise I would just use List<T> directly.

这篇关于名单&LT; BusinessObject的&GT;或BusinessObjectCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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