Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be a good replacement for C++ vector in C#?

Tags:

c++

c#

.net

I'm working on improving my skills in other languages, coming from using c++ as my primary programming language. My current project is hammering down C#.net, as I have heard it is a good in-between language for one who knows both c++ and VB.net.

Typically when working with an unknown number of elements in c++ I would declare my variable as a vector and just go from there. Vectors don't seem to exist in c#, and in my current program I have been using arraylists instead, but I'm starting to wonder if it's a good habit to use arraylists as I read somewhere that it was a carryover from .net 1.0

Long question short- what is the most commonly used listing type for c#?

like image 819
Holman716 Avatar asked Jan 30 '10 01:01

Holman716


People also ask

What can be used instead of vector in C?

A block and realloc() is all well and good but you really need a data structure that features like a linked list.

What can I use instead of vectors?

Alternatives to the Vector Class If a thread-safe implementation of List interface is required, we can either use CopyOnWriteArrayList class, which is a thread-safe variant of the ArrayList or synchronize ArrayList externally using the synchronizedList() method of the Collections class.

Can I make a vector in C?

Vectors are a modern programming concept, which, unfortunately, aren't built into the standard C library. They are found in C++, which is an object oriented programming extension of C. Essentially, vectors replace arrays in C++.

Does C have a vector library?

C Vector Library A simple vector library for C. This libary's vectors work in a similar manner to C++ vectors: they can store any type, their elements can be accessed via the [] operator, and elements may be added or removed with simple library calls.


1 Answers

If you target pre .NET 2.0 versions, use ArrayList

If you target .NET 2.0+ then use generic type List<T>

You may need to find replacements for other C++ standard containers, so here is possible mapping of C++ to .NET 2.0+ similar types or equivalents:

std::vector - List<T>
std::list - LinkedList<T>
std::map - Dictionary<K, V>
std::set - HashSet<T>
std::multimap - Dictionary<K, List<V>>
like image 180
mloskot Avatar answered Sep 21 '22 22:09

mloskot