开发者

How to define a collection of my class in C#?

开发者 https://www.devze.com 2023-04-12 08:26 出处:网络
I have encountered a problem on how to define a two dimensional list(or array). Here is my data: line1line2...lineN

I have encountered a problem on how to define a two dimensional list(or array). Here is my data:

line1                             line2                       ...          lineN

element1: name, phone, addr    开发者_运维知识库 element1: name, phone, addr
element2: name, phone, addr     element2: name, phone, addr
...                              ...
elementN: name, phone, addr     elementN: name, phone, addr

The problem is that we don't know the exact lines and elements in each line. I tried to define the class People, containing members of name, phone, addr, etc, and use:

list<People> info = new list<People>();

That only defined one line - how can I define multiple lines?


People person = new People;    
person.Address = "Address";
person.Phone = "11111";

List<People>() info = new List<People>();
info.Add(person);

will add a new instance of your People class (although Person would be a better name)

or potentially better would be a Dictionary with a suitable key


I'm not really sure what you are after but how about a list of dictionary, i.e. List<Dictionary<string, object>>? This will allow you to represent each person as a dictionary with a differing number of attributes.

Ok, after your question was edited is this what you are after: List<List<People>>?


You should have something like:

class SomePeople
{
    SomePeople()
    {
        people = new list<People>();
    }

    public list<People> people;
}

List<SomePeople> info = new List<SomePeople>();

Or something like that, you get the point...


Try to use indexers here is the link from Microsoft Development Center http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx || http://msdn.microsoft.com/en-us/library/2549tw02.aspx


Another alternative would be to use a list of arrays, so you would declare :

List<People[]> peopleList = new List<People[]>();

And then use it like so :

peopleList.Add(new People[2]
{
    new People { name = "name", phone = "123"...},
    new People { name = "name", phone = "123"...}
});

peopleList.Add(new people[3]
{
   // x 3 initializers
});

Hope that is equally as helpful!


Create a PeopleCollection class that extend Collection:

public class PeopleCollection : Collection<People>
{

}

It provides the base class for a generic collection. From msn documentation: The Collection class provides protected methods that can be used to customize its behavior when adding and removing items, clearing the collection, or setting the value of an existing item. (http://msdn.microsoft.com/en-us/library/ms132397.aspx)

Then you can define a list of People Collection, or a Dictionary, it depends on your object's access need. Like:

List morePeoples = new List();

Here you can put more "line", as you have described. Each "line" (list element) contains a collection of peoples.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号