Array of Variants in c#

3.4k views Asked by At

In c#, I want declare a function with a Dinamic array that receive any type of data primitive types like (string, integer, doubles, datetimes ....) but this types are not a Object , just a basic types. There is someway to do this ?

2

There are 2 answers

0
Rodrigo Zimmermann On BEST ANSWER

The code above works, but Have a Limitation, he don't works with diferentes types of data like a

MyFunc(new[] {"alpha", 123, 01-02-2002});

This code below works with string, int and date

public bool ArrayVariant(string pQuery, ICollection collection)
{

        foreach (var item in collection)
        {
             //do something with item
        }
}

and to call function

ArrayVariant( "any data", new dynamic[]  {"teste", 0, DateTime.Now});
0
ArgusMagnus On

How about

public static void MyFunc(params object[] items)
{
    foreach (object item in items)
        // Do somthing with item
}

Call it like

MyFunc("test", 0, DateTime.Now);