user35288
user35288

Reputation:

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.

Upvotes: 3

Views: 1749

Answers (7)

user35288
user35288

Reputation:

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#

Upvotes: 0

Tamas Czinege
Tamas Czinege

Reputation: 121314

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.

Upvotes: 0

Harper Shelby
Harper Shelby

Reputation: 16583

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

Upvotes: -1

Qwertie
Qwertie

Reputation: 17186

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500585

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?

Upvotes: 5

FlySwat
FlySwat

Reputation: 175593

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

Upvotes: 1

Martin v. L&#246;wis
Martin v. L&#246;wis

Reputation: 127467

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?

Upvotes: 3

Related Questions