Search Unity

C # Is it possible to create a method that can resize multiple arrays with prams?

Discussion in 'Scripting' started by Gold_Rush, Dec 20, 2020.

  1. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    for example:

    Code (CSharp):
    1. int[] array1 = new int[30];
    2. float[] array2 = new float[30];
    3. byte[] array3 = new byte[30];
    4.  
    5. newsize=50;
    6. ResizeArrays(newsize, array1,array2,array3);
    7.  
    8. public static void ResizeArrays(int newsize,params object[] obj){
    9.    for(int i=0;i<obj.len;i++)[
    10.       ... each array resize code;
    11.    }
    12. }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Why don't you use List<> instead? They have no fixed size so ne need for resize.
     
  3. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,065
    You can't resize arrays, resizing will always create a new array and the reference in your variable needs to be replaced with the new one.

    If you look at the Array.Resize method, you see that the array argument uses
    ref
    :
    Code (CSharp):
    1. public static void Resize<T> (ref T[]? array, int newSize);
    Only by passing the array as a ref parameter can the original variable be updated with the new array.

    There's no way to combine
    ref
    and
    params
    , so your code won't work. You could create multiple overloads that each take 1, 2, 3, etc ref arrays, which would work like
    params
    but with an upper limit of arrays you can pass.
     
    Gold_Rush likes this.
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
  5. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    So I have "Invested" few hours to solve this puzzle.
    This is the best I got, if you can do better please post your solution.
    Code (csharp):
    1.  
    2. using System;
    3.  
    4. public class Program {
    5.  
    6.    public delegate ref Array GetArrayRefFunc();
    7.  
    8.     public static void ResizeArrays(int newsize,params GetArrayRefFunc[] obj){
    9.        for(int i=0;i<obj.Length;i++){
    10.           ref Array o = ref obj[i]();  
    11.           Array newArray = Array.CreateInstance(o.GetType().GetElementType(),newsize);
    12.           o.CopyTo(newArray,0);
    13.           o = newArray;
    14.        }
    15.    }
    16.  
    17.    static Array arr1 = new int[]{1,2,3,4};
    18.    static Array arr2 = new int[]{1,2,3};
    19.    static int[] arr3 = new int[]{1,2,3};
    20.  
    21.    public  static void Main()
    22.    {
    23.        Console.WriteLine( (arr1 as Array).Length);
    24.        Program.ResizeArrays(50,()=> ref arr1,()=> ref arr2);
    25.        Console.WriteLine( (arr1 as Array).Length);
    26.      
    27.        //Program.ResizeArrays(50,()=> ref arr3); <-----this not compile (not of type Array)
    28.      
    29.        Array arrLocal = new int[]{1,2,3};
    30.        //Program.ResizeArrays(50,()=> ref arrLocal);// <-----this not compile (local array)
    31.      
    32.    }
    33.  
    34. }
    35.  
     
    Gold_Rush likes this.
  6. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    To koirat
    Im think this is highest possible result. At the moment this is the maximum that can be squeezed out of the capabilities of C #

    Perhaps this method can still be implemented using reflection. If pass the array names to the method as strings.
     
  7. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    Yes you could probably do this with reflection even without names.
    But I don't think it is worth the effort.
     
    Gold_Rush likes this.
  8. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    I also think not worth it. Thanks for the example!

    This is my habit from J2ME of not using reference data types. Limit the number of classes and structures. And use simple data types.
     
  9. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    [QUOTE="This is my habit from J2ME of not using reference data types. Limit the number of classes and structures. And use simple data types.[/QUOTE]

    Except that an array is a reference data type, like strings (which are array of chars). Seems a strange habit to me - data types are there for a reason. But everyone their own dogma I guess.
     
  10. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    I personally also use array whenever I can/see fit.
    There are some pros of doing so like:

    Fast read write. (no bounds checking in most cases etc.)
    You can change value of single field of struct in array. array[0].field = value (without copying a struct)
    You can "ref" on single element this will allow you to pass single element to method by ref. ( ref array[0] )
    Probably better for GC.
     
    Gold_Rush likes this.
  11. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Yes, and in the (bad) old days, we simply declared a static var as an array[0] of byte at the the heap/code boundary, switched off bounds checking for the compiler, and were then able to willy nilly modify memory locations in the heap; we could even patch the code itself by using negative array indices; or we would write to hardware that was mapped to some memory locations (did that on the Apple // to write to a serial port).

    Today, we don't do that any more because code needs to be serviced, memory is protected and we all learned that data structures are a significant part of good design. There are very, very few problems today that require old-school brute-force methods that can't be solved in a more elegant, readable und serviceable way. They do exist, but I've never run across one in Unity. If you use arrays simply because you think that it may give you a leg up avoiding GC, you are probably doing something wrong.
     
    Kurt-Dekker likes this.
  12. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    I use it sometimes for the same reasons.