Does 2005 C# have STL?

P

Peter Olcott

Does the 2005 release of Visual Studio provide either STL (the C++ Standard
Template Library) or an equivalent for C#? I am most interested in the 2005 C#
equivalent of std::vector.
 
J

Jon Skeet [C# MVP]

Peter Olcott said:
Does the 2005 release of Visual Studio provide either STL (the C++ Standard
Template Library) or an equivalent for C#? I am most interested in the 2005 C#
equivalent of std::vector.

Look at System.Collections.Generic.List<T>
That's the closest equivalent, I believe.
 
R

Randy A. Ynchausti

Peter,
Does the 2005 release of Visual Studio provide either STL (the C++
Standard Template Library) or an equivalent for C#? I am most interested
in the 2005 C# equivalent of std::vector.

In general, there are several classes in C# that provide the functionality
of std::vector. However, which to use depends on what storage model and
behavior you want to need. If you need fast lookup of random data in the
"vector", you might want to use:

System.Collections.Specialized.HybridDictionary

If your application needs a plain vanilla queue-like behavior, you might
start with:

System.Collections.Queue

If your application needs a plain vanilla array-like behavior where you
iterate over the elements, you might use:

System.Collections.ArrayList

If your application needs the list to be sorted, you might use:

System.Collections.SortedList

There are other collection, link-list, etc. classes, but I am sure you get
the idea. The one you pick depends on the characteristics that are most
important to the application. Start in the System.Collections namespace. I
hope that helps.

Regards,

Randy
 
R

rossum

Does the 2005 release of Visual Studio provide either STL (the C++ Standard
Template Library) or an equivalent for C#? I am most interested in the 2005 C#
equivalent of std::vector.
VS 2005 is .NET 2.0 so it has generics. The closest to std::vector<t>
is System.Collections.Generics.List<T> which is contiguous storage,
random acces with an indexer ([]) and easy to add new items at the end
but not in the middle. THere are other containers in
System.Collections.Generics which you might want to look at as well,
depending on your exact requirements.

rossum
 
R

Randy A. Ynchausti

Peter,
Yeah, but, they all changed with 2005 to have generics too right?

There is a new namespace, System.Collections.Generic. The Generics flavor
of the collection classes mainly provide a higher degree of type safety.
For example, let's say we want to have a dictionary that uses an integer as
the key and a DateTime as the value. The non-Generic version might be coded
as:

System.Collections.Specialized.HybridDictionary x = new
System.Collections.Specialized.HybridDictionary();
x.Add(1, DateTime.Now);
x.Add("Hello", "World"); // compiles and runs without errors

The non-generic version allows the wrong type of data to be added to the
collection at run-time. Also note that there are no compile-time issues.
The generics version won't allow you to even compile your application:

System.Collections.Generic.Dictionary<int, DateTime> y = new
System.Collections.Generic.Dictionary<int, DateTime>();
y.Add(1, DateTime.Now);
y.Add("Hello", "World"); // compiler error 1) can't convert string to int
and 2) can't convert string to DateTime

So one benefit of Generics is the type safety. However, there are simple
ways to guarantee type safety and use the non-generic incantations of the
collections.

Regards,

Randy
 
J

Jon Skeet [C# MVP]

Randy A. Ynchausti said:
There is a new namespace, System.Collections.Generic. The Generics flavor
of the collection classes mainly provide a higher degree of type safety.

Well, type-safety *and* performance. If you create an ArrayList and
call Add ((byte)10) to it a million times, you'll use up a *lot* more
memory than using List<byte>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top