Frink
Frink

Reputation: 221

C# object or array

I am litle new with c#.

I need to store data in variable,but I am not sure what is best method for that?

For example if I make object:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
    //Other properties, methods, events...
}

In code I can define for example

Person person1 = new Person("Leopold", 6);

But what if I have 10000 persons, I must define all of them(person1,person2,etc)? That is not good idea I think. Can I use array or something like that,and how to get that person witch have name Leopold.What is best method to store big collection of data ,and have good access for them. (For example good will be if I could use something like person[somenumber].name or some similar)

Sorry, one more time i am new in this...

Thnx

Upvotes: 1

Views: 1183

Answers (5)

toplel32
toplel32

Reputation: 1174

You should only use an array if you can ensure that a certain boundary will never be exceeded. This is typically only approperiate for code with an internal purpose. A set of business objects can accumulate to any size, so you want to use lists instead. Lists have no explicit boundary because each element in a list contains a reference to the next one, this can continue until the list runs out of memory.

Also only use constructors for classes with behavior, such classes only need a few arguments to initialize. Classes with a sole purpose of carrying data can contain an arbitrary amount of fields to describe something, passing an argument for each field is slow and it looks unreadable so use this notation instead:

new Person() { Name = "Leopold", Age = 21 };

Since all the properties are mutable with no validation, there is no need to use properties rather than fields. So you might want to use this instead:

public class Person
{
    public string Name;
    public int Age;
}

Because this class represents a real world object, I named the fields with PascalCase, but remember to use camelCase for any other type of class. This is just a matter of code convention.

If you want to find items in a list, you can use the following methods:

  • Find
  • FindAll
  • Exists

If you need more power, then you need Linq.

Upvotes: 0

Adil
Adil

Reputation: 148140

Well it depend on what you need if you have fixed number of record you may use array but if you are not sure about number of element then generic List would be my choice. As you do not have to take care of the size of List<T> the size is automatically maintained by List as elements are added or removed. On the other hand arrays are fixed size and you may end up with creating new array when you have to store more elements then the size of array.

List<Person> personList = new List<Person>();
personList.Add(new Person("AnyName1", 7));
personList.Add (new Person("AnyName2", 4));

Upvotes: 1

rosko
rosko

Reputation: 474

My suggestions for your problem:

List<Person> persons = new List<Person> 
{ 
    new Person("Leopold", 36),
    new Person("Henry", 46),
    new Person("Joe", 56),
    new Person("Harrison", 66) 
};

var findLeopold = persons.Find(p => p.Name == "Leopold"); // find Leopold in list
var firstPerson = persons[0]; // access by person[someNumber]

Upvotes: 1

Jens
Jens

Reputation: 477

You might be looking for a generic list, which are very easy to use.

List<Person> peopleList = new List<Person>();
peoplelist.Add(new Person("Leopold", 6));

And if you want the person named leopold

Person leopold = peoplelist.FirstOrDefault(person => person.Name == "Leopold");

This is linq so you should probably read up on that.. Your other alternative is to use a dictionary which will give you indexed lookups which you mentioned.

Upvotes: 2

Bob Tway
Bob Tway

Reputation: 9603

You need a collection object. List is the most versatile:

List<Person> people = new List<Person>();

You can then do operations like this:

people.Add(new Person("Bob", 7));
people.Add(new Person("Alice", 4));

There are plenty more useful operations you can perform on a List. Here's a good tutorial: http://www.dotnetperls.com/list

Upvotes: 1

Related Questions