views:

799

answers:

7
+2  Q: 

Using Lists in C#

I am an upper level Software Engineering student currently in a Data Structures and Algorithms class. Our professor wants us to write a program using the List structure found in the C++ STL. I have been trying to use C# more and more, and was wondering if the ArrayList structure in .NET is a good substitute for the STL List implementation.

+3  A: 

You should be able to answer this question yourself. What is the implementation strategy used in STL lists? What is the one of ArrayList? Likewise, what is the abstract API presented by STL list (in terms of operations provided)? Compare this to STL List: what does the one provide that the other doesn't?

Martin v. Löwis
+1  A: 

if the STL List uses templates, you might want to look at the generic List class in System.Collections.Generic.

FlySwat
+5  A: 

Unless you're stuck with .NET 1.1, use List<T> instead of ArrayList. But what are you fundamentally concerned about? Suppose you didn't have List to refer to - what do you need the appropriate data structure to do?

Jon Skeet
A: 

The ArrayList class is somewhat deprecated. It is from the .NET 1.0 times when generics did not exist yet.

You should use System.Collections.Generic.List instead. Like this:

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
System.Console.WriteLine(myList[0]);

And yes, both of them are good substitutes. You should use the generic List though, since it is type safe and potentially quicker.

DrJokepu
A: 

Um, C++ STL doesn't have a structure called "List". I think there is a "list", which is a linked list. C#'s List, in contrast, is analagous to C++'s vector.

Qwertie
Yep, mistyped, meant std::list<T>
W_P
A: 

The closest C# analogue of the std::list is System.Collections.List. Both are generic collections, and implement the standard list-type actions.

Harper Shelby
A: 

Thanks everyone

quertie, i mistyped and meant list instead of List...

the assignment is to use std::list to add polynomials using a list of simple structs, a struct that would hold the coefficient and the power of x...easy enough, I know, but since the class is supposedly language-independent, I wanted to try to use c#

W_P