简体   繁体   中英

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.

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.

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