0

Perhaps I am thinking too much along the lines of dynamic duck-type langagues (python and JavaScript). But how do I make the array below that is declared at compile time to shrink to a smaller size?

       public struct Centroid
        {
            public float x;
            public float y;
            public int count;
            public float strength;
        }
centers = new Centroid[1024];

and now I want to re-size it at runtime with a computed value: starCount

centers = new Centroid[starCount];

UPDATE

[Yes, I would like to use List<> generics] but I am stuck in that I need to read a GPU array and move it to the C# array, and I also need an array since I need to send the results to Matlab via the .net interface.]

Dr.YSG
  • 7,171
  • 22
  • 81
  • 139
  • 3
    you want a variable-sized array? why not use a list, or some other data structure? – DLeh Jul 07 '15 at 13:54
  • 1
    Use a `List` that is dynamically sized. Else you can create a smaller array and copy it. – Matt Burland Jul 07 '15 at 13:55
  • Just search for your title and you will get lots of [results](https://www.google.ca/search?q=resize+C%23+array+of+objects&oq=resize+C%23+array+of+objects&aqs=chrome..69i57j69i60l2&sourceid=chrome&es_sm=122&ie=UTF-8) – Habib Jul 07 '15 at 13:55
  • Chances are, you wouldn't use an array for this; you'd use a List or some other dynamically-resizable enumerable type, or you'd use two separate arrays. Technically, what you're doing is legal; it will just change `centers` to point to the newly-created array instead of the old array. – Shotgun Ninja Jul 07 '15 at 13:57
  • I am a bit stuck, I need the arrays for compatiblity with CUDAfy and with Matlab. Yes, I know about Lists. But I need the Array to populate the array and also to send it up to Matlab. – Dr.YSG Jul 07 '15 at 14:17

0 Answers0