0

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();
}
1
  • Why are you trying to pass parameters like this rather than using named parameters? The problem you have is that the second item in array is of type string[] and if you call ToString on this (which WriteLine does behind the scenes) it will just print its type. If you don't want it to do this you need to change your code. Commented Jun 15, 2017 at 9:24

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Did not think to make it an IEnumerable (went with string[]) good thinking!
2

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();
    }

Comments

0

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

Comments

0

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.

2 Comments

Defeats the purpose of params. If I add 1 more param (eg. TestArray(combined.ToArray(), "something"); the code breaks. Your method does work but it essentially only accepts 1 collection, so params won't be needed.
Thanks @EpicKip !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.