Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have some old C code with some basic data structure: dynamic array.
I want to convert it into C# code.

Which type in C# I should map to?

What I have tried:

tuitively ArrayList in C# is the answer. try to see if there is a better answer.
Posted
Updated 18-Jun-16 20:19pm
Comments
PIEBALDconsult 19-Jun-16 1:11am    
That may be, but ArrayList isn't very good. Try a generic List<> instead.
Sergey Alexandrovich Kryukov 19-Jun-16 2:00am    
To get a solution, please write a sample of C function signature. "Dynamic array" sounds ambiguous. What is it, exactly?
—SA

Not an ArrrayList, for sure!
The problem is that a dynamically sized array in C (which is what I assume you are using since C doesn't have a specific dynamic keyword) isn't a "fixed" element size, and ArrayList is - it's an Array of object which are effectively pointers and so fixed at 32 or 64 bits depending on system word width.
The C code could create an array of 100 elements, each char (100 bytes), each int (200, 400, or 800 bytes), pointers (400 or 800 bytes) or even a struct (100 * sizeof(myStruct) bytes) so mapping that to an ArrayList will not work.
You need to map C code to an array or List<T> of the appropriate width datatype in C# for it to work, if you want to call the C code from within the C# - it is critical to get the size matching.
If you are rewriting the C into C#, then it's a lot less important to match the element size, but an ArrayList is still the wrong solution: it is obsolete and has been for a long time - it was superseded by the List<T> in .NET V2.0!
 
Share this answer
 
Comments
Southmountain 19-Jun-16 22:55pm    
Thank you very much!
OriginalGriff 20-Jun-16 2:19am    
You're welcome!
Please see open-source P/Invoke Interop Assistant:
Managed, Native, and COM Interop Team — Download P/Invoke Interop Assistant.

—SA
 
Share this answer
 
Comments
Southmountain 19-Jun-16 22:55pm    
Thank you very much for the link. it contains a lot of information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900