简体   繁体   中英

How to get string array values, i.e. passed as a parameter in an object array?

I am trying to pass string array and other parameters in an object array,and on other side I want to retrieve this parameter values and display them, but I am not able to retrieve the string array values,rather it displays the type of the string array.

static void Main(string[] args)
{
    string[] test = {"t1","t2","t3"};

    TestArray(1,test,"Hello");
}

static void TestArray(params object[] array)
{  
    foreach(var value in array)
    {
        Console.WriteLine(value);                    
    }
    Console.ReadLine();
}

You're printing all values as string. Array.ToString() will return $elementType[] , so System.String[] in your case.

You'll need to test if value is an IEnumerable , and if so, iterate over it and print its members' values, recursively.

static void TestArray(params object[] array)
{  
    PrintValue(value);       
}

public void PrintValue(object value)
{
    var enumerable = value as IEnumerable;
    if (enumerable != null)
    {
        foreach (var subvalue in enumerable)
        {
            PrintValue(subvalue);
        }
    }
    else
    {
        Console.WriteLine(value.ToString());
    }
}

Do note that this still can't print complex types, and in that case, will just output its type name again.

Try this:

    static void Main(string[] args)
    {
        string[] test = { "t1", "t2", "t3" };

        TestArray(1, test, "Hello");
    }

    static void TestArray(params object[] array)
    {
        foreach (var value in array)
        {
            if (value is IEnumerable<object>)
                foreach (var element in value as IEnumerable<object>)
                    Console.WriteLine(element);
            else
                Console.WriteLine(value);
        }
        Console.ReadLine();
    }

You have to check if the value is an array and loop over it:

public static void DisplayParams(params object[] parameters)
{
    foreach(var param in parameters)
    {
        var arr = param as string[];
        if( arr  != null)
        {
            foreach(var value in arr)
            {
                Console.WriteLine( value );
            }
        }
        else
        { 
            Console.WriteLine( param );
        }
    }
}

This part:

var arr = p as string[];
if( arr  != null)

will check if its an array and will loop over it when it is.

Of course when you insert other types this won't work, so some validation would be wise

One option to consider:

    static void Main(string[] args)
    {
        string[] test = { "t1", "t2", "t3" };

        List<object> combined = new List<object> {1};
        combined.AddRange(test);
        combined.Add("Hello");

        TestArray(combined.ToArray());
    }

    static void TestArray(params object[] array)
    {
        foreach (var value in array)
        {
            Console.WriteLine(value);
        }
        Console.ReadLine();
    }

In short, you merge all the values into a single array before passing that array into the method.

If you went this route, consider removing the params keyword - so it makes it literally impossible for someone to call it the original way you had it. It forces them to think about how they want IEnumerables to be handled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM