Add double[] to to custom stack in C#

My problem is that I cannot push a double[] to my custom stack.

if (int.TryParse(item[0], out intTemp))
{
   customStack.Push(parts.Skip(1).Select(int.Parse).ToArray());
}
else if(double.TryParse(item[0], out doubleTemp))
{
customStack.Push(parts.Skip(1).Select(double.Parse).ToArray());
}

The first if statement works, but the second gives me an exception – CS1503: Argument 1: cannot convert from ‘double[]’ to ‘int’.

The problem is I’m not even trying to add a ‘double[]’ into ‘int’.

Here is my Push method:

public void Push(params T[] items)
    {
        foreach (T item in items)
        {
            this.stackData.Add(item);
            Console.WriteLine($"Element {item} added to stack.");
        }
    }

Help will be appreciated.

Answer

customStack seems to be a CustomStack<int>. So your Push method expects a params int[] argument.

parts.Skip(1).Select(double.Parse).ToArray() returns a double[] that the compiler is now trying to convert into the first element of the int[] items parameter, which leads to your error message.

Leave a Reply

Your email address will not be published. Required fields are marked *