D
D
Denis2015-11-10 09:20:41
C++ / C#
Denis, 2015-11-10 09:20:41

How to convert string[] array to double[] array?

Good afternoon!
I am writing a method that receives 2 string[] arrays as input. I need to convert a string to a double (or int), and I already know that only numbers are real there, but VS gives an error Cannot implicitly convert type 'int' to 'string'
Here is the code:

private void GrathMath(string[] x, string[] y)
        {
            for (int i = 0; i<x.Length; i++)
            {
                x[i] = double.Parse(x[i]);
            }
        }

Tell me, please, how can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
georgich, 2015-11-10
@darkmayers

Greetings.
Here is the answer on the msdn forum .

double [] doubleArray = new double[stringArray.Length]

for(int i=0; i<stringArray.Length; i++)
{
     doubleArray[i] = Double.Parse(stringArray[i]);
}

or LINQ
double[] doubleArray = stringArray.Select<string, double>(s => Double.Parse(s)).ToArray<double>();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question