-1

I call FunctionB passing a string array using generic method like this.

string[] array1 = {"data1", "data2", "data3"};

void FunctionA ( )
{
    FunctionB ( array1 );
}

void FunctionB <T> (T arg)
{
    print (typeof(T));
}

I got the data type of arg. Is there any way to have array1's data elements in FunctionB ?

halfer
  • 19,824
  • 17
  • 99
  • 186
David Kim
  • 41
  • 5
  • What programming language is this? – Raymond Chen Dec 25 '21 at 14:34
  • It is C# using in Unity. – David Kim Dec 26 '21 at 08:13
  • Don't answer in a comment. Update the tags, please. – Raymond Chen Dec 26 '21 at 21:06
  • Does this answer your question? [C# generic constraint: Array of Structs](https://stackoverflow.com/questions/20379573/c-sharp-generic-constraint-array-of-structs). `void FunctionB (T[] arg)`. – Raymond Chen Dec 26 '21 at 21:53
  • I am kind of new to C#. I am not even sure my code is correct. What I want is,,, how to call FunctionB with different data type of parameter, such as string[] or string[,]... It seems not easy for beginners. – David Kim Dec 27 '21 at 00:30
  • What would it mean to generically accept both `string[]` and `string[,]`? The member accesses are different. – Raymond Chen Dec 27 '21 at 05:50
  • FunctionB does not know what data type of parameter comes in. But my code is OK with compile even if FunctionA calls FunctionB with a parameter which is string[ , ]. So my question is,,, can I get data elements of passing array in FunctionB no matter what the parameter is? Sorry I am not good English speaker. – David Kim Dec 28 '21 at 00:25
  • It depends on how you want to access the elements. Are you planning to iterate over them? What order do you want to iterate the 2-dimensional array? What about 5-dimensional arrays? Is it always strings? Could it be a 9-dimensional array of ints? What would it mean to access the 3rd element of a 2-dimensional array? – Raymond Chen Dec 28 '21 at 03:19
  • First of all, thanks for your time and comments. Actually, FunctionB will have 3 types parameters which are 2, 3 or 4 dimensional string arrays. It does not know which one passes in. FunctionB can show what dimension the parameter has. But I can't find how to read data elements from the passed array. Now I'm not sure if my code can do it. – David Kim Dec 28 '21 at 04:26
  • One way is to probe the parameter at aruntime. `if (arg is string[] dim1) { dim1[0], dim1[1], etc. } else if (arg is string[,] dim2) { dim2[0,0], dim2[0,1], etc. } else if (arg is string[,,] dim3) { etc }`. Another way is to write three overloads. `void FunctionB(string[] arg); void FunctionB(string[,] arg); void FunctionB(string[,,] arg);` – Raymond Chen Dec 28 '21 at 05:17
  • I will try your suggestions, even though I do not quite understand it. Thanks for everything :) – David Kim Dec 28 '21 at 08:16
  • It's hard to answer your question because you haven't really said what you want FunctionB to do when it gets a string[] or a string[,] or a string[,,]. The arrays have different numbers of dimensions, so there's no obvious "common" thing you want to do to all of them. Maybe you could write a FunctionB that shows what it is you want to do generically. e.g. `void FunctionB(T args) { foreach (string s in args) DoSomething(s)); }` – Raymond Chen Dec 28 '21 at 13:38
  • What I want is how to detect what kind of data the parameter is. Like I said, FunctionA calls FunctionB with the parameter which can be string[], string[,] or string[,,]. Maybe it's impossible with my code. I was wondering if it's possible with that simple code. – David Kim Jan 01 '22 at 01:59
  • If you just want to detect the type, then use `is`. `if (arg is string[]) { /* the argument is a string[] */ }` But your question was "Is there any way to have array1's data elements in FunctionB ?" which makes it sounds like you want to access the elements of the array, not just determine its type. – Raymond Chen Jan 01 '22 at 03:32

2 Answers2

1

You can create another method, which has an array as a parameter and spring each element of the array. Example:

void FunctionB <T> (T[] arg)
{
    foreach(T item in arg)
    {
        print (item);
    }
}
0

I supposed you need to use pattern matching. Here is how to do it:

static class Program
{
    static void FunctionB<T>(T arg)
    {
        if (arg is string[] array)
        {
            foreach (var item in array)
            {
                Console.WriteLine(item);
            }
        }
    }
    static void Main(string[] args)
    {
        // TODO: Put code here
        string[] array1 = { "data1", "data2", "data3" };

        FunctionB(array1);
        //data1
        //data2
        //data3
    }
}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133