vectors in C#?

Started by
9 comments, last by Anexa85 15 years, 8 months ago
I have been programming in C++ and was wondering what you use in place of vectors in C#?
Advertisement
Usually, arraylists (System.Collections.ArrayList or more recently System.Collections.Generic.ArrayList System.Collections.Generic.List).
System.Collections.Generic.ArrayList Is the closest thing to a linked list that you can get in C# with out creating your own classes and functions.
Quote:Original post by ToohrVyk
Usually, arraylists (System.Collections.ArrayList or more recently System.Collections.Generic.ArrayList).


Don't use ArrayList anymore. It's outdated, and performs poorly when used with value types (constant boxing and unboxing).

The new version that makes use of generics is System.Collections.Generic.List<T>, and takes the type of the objects in the list as the generic parameter.

Also, using arrays in C# isn't looked down upon like it is in C++. Arrays in C# are safe and easy to use, and don't suffer from the many problems that afflict their C++ counterparts.
Mike Popoloski | Journal | SlimDX
Thanks for the replies. So you're saying I should just use arrays then? I know in C++ that arrays are not good for a lot of situations where the elements have to be manipulated a lot, moved around, or that need to change in size. Do arrays in C# handle this or something?
Quote:Original post by Anexa85
Thanks for the replies. So you're saying I should just use arrays then? I know in C++ that arrays are not good for a lot of situations where the elements have to be manipulated a lot, moved around, or that need to change in size. Do arrays in C# handle this or something?


No, arrays are still fixed size in C#. If you don't know the size of the list, but just need to add or remove objects on the fly, List<T> will be your best bet.
Mike Popoloski | Journal | SlimDX
Quote:Original post by Mike.Popoloski
Don't use ArrayList anymore. It's outdated, and performs poorly when used with value types (constant boxing and unboxing).


Darn, I thought for a second there that the generic version was still called 'ArrayList' (like in Java, where 1.5 introduced a generic ArrayList). I did mean the generic System.Collections.Generic.List, too. I apologize for the confusion.
Quote:Original post by ToohrVyk
Quote:Original post by Mike.Popoloski
Don't use ArrayList anymore. It's outdated, and performs poorly when used with value types (constant boxing and unboxing).


Darn, I thought for a second there that the generic version was still called 'ArrayList' (like in Java, where 1.5 introduced a generic ArrayList). I did mean the generic System.Collections.Generic.List, too. I apologize for the confusion.


No problem, I noticed it right away, I just wanted to make sure that the OP didn't get confused. After GoogolPL3X posted the same thing, I began to wonder if there actually was such a thing as a generic ArrayList. A quick Google solved the problem, however. [smile]
Mike Popoloski | Journal | SlimDX
Judging from the design of .NET, I suspect that they though it was too confusing to have an ArrayList container but no other kind of list (besides, for cache reasons, in a reference-semantics language an ArrayList will generally behave faster than a LinkedList anyway) and decided to call it a plain old List.
Quote:Original post by Googol PL3X
System.Collections.Generic.ArrayList Is the closest thing to a linked list that you can get in C# with out creating your own classes and functions.


BTW, besides the name, List and ArrayList are not linked list. If you need linked list behaviour go for System.Collections.Generic.LinkedList.

Regards,
Andre
Andre Loker | Personal blog on .NET

This topic is closed to new replies.

Advertisement