Convert string[] To Decimal[],int[],float[] .double[] in c#

c#, silverlight

Solution

You can't convert a string[] straight to a decimal[] like that - all the elements have to be individually converted to the new type. Instead, you can use Array.ConvertAll

string[] txt1 = new string[]{"12","13"};
decimal[] dec1 = Array.ConvertAll<string, decimal>(txt1, Convert.ToDecimal);

And similarly using `Convert.ToInt32`, `Convert.ToSingle`, `Convert.ToDouble` for the `Converter<TInput,TOutput>` argument to produce int[], float[], double[], substituting in the correct type arguments to ConvertAll

EDIT: As you're using silverlight, which doesn't have ConvertAll, you'll have to do it manually:

decimal[] dec1 = new decimal[txt1.Length];
for (int i=0; i<txt1.Length; i++) {
    dec1[i] = Convert.ToDecimal(txt1[i]);
}

Problem

``` string[] txt1 = new string[]{"12","13"}; this.SetValue(txt1, v => Convert.ChangeType(v, typeof(decimal[]), null)); ``` it throws an error - Object must implement IConvertible. I also want a code to convert string[] To Decimal[],int[],float[] .double[]

Original source