Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# array vs generic list [duplicate]

i basically want to know the differences or advantages in using a generic list instead of an array in the below mentioned scenario

class Employee
{
    private string _empName;

    public string EmpName
    {
       get{ return _empName; }
       set{ _empName = value; }
    }
}

1. Employee[] emp 
2. List<Employee> emp

can anyone please tell me the advantages or disadvantages and which one to prefer?

like image 660
L G Avatar asked Mar 12 '10 06:03

L G


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


3 Answers

One big difference is that List<Employee> can be expanded (you can call Add on it) or contracted (you can call Remove on it) whereas Employee[] is fixed in size. Thus, Employee[] is tougher to work with unless the need calls for it.

like image 149
Thomas Avatar answered Sep 21 '22 03:09

Thomas


The biggest difference is that arrays can't be made longer or shorter once they're created. List instances, however can have elements added or removed. There are other diffs too (e.g. different sets of methods available) but add/remove is the big difference.

I like List unless there's a really good reason to use an Array, since the flexibility of List is nice and the perf penalty is very small relative to the cost of most other things your code is usually doing.

If you want to dive into a lot of interesting technical detail, check out this StackOverflow thread which delves into the List vs. Array question in more depth.

like image 34
Justin Grant Avatar answered Sep 21 '22 03:09

Justin Grant


With the generic list, you can Add / Remove etc cheaply (at least, at the far end). Resizing an array (to add/remove) is more expensive. The obvious downside is that a list has spare capacity so maybe wastes a few bytes - not worth worrying about in most cases, though (and you can trim it).

Generally, prefer lists unless you know your data never changes size.

API-wise, since LINQ there is little to choose between them (i.e. the extra methods on List<T> are largely duplicated by LINQ, so arrays get them for free).

Another advantage is that with a list you don't need to expose a setter:

private readonly List<Foo> items = new List<Foo>();
public List<Foo> Items { get { return items; } }

eliminating a range of null bugs, and allowing you to keep control over the data (especially if you use a different IList<> implementation that supports inspection / validation when changing the contents).

like image 28
Marc Gravell Avatar answered Sep 22 '22 03:09

Marc Gravell