Home > database >  Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists
Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists

Time:12-03

I know its normally obvious where the conversion between double and int has gone wrong but im using recursion methods to add up the sum of a list to 1 decimal point but i cannot seem to find where the error is. There is no error when i use console.WriteLine but when i use return (which i would like) it comes up with the error.


double[] arr = { -1.103f, 2.2f, 3.1f, 10.0f, 15.0f, 23.1f, 22, 12f };

List<double> values = new List<double>(arr.Length);

foreach (double i in arr)
{
    values.Add(i);
}

static double sum(List<double> values, int position = 0)
{
    if (values[position] == values[values.Count - 1])
    {
        return values[values.Count - 1];
    }
    return Math.Round(values[position], 2)   sum(values, position   1);
}

return sum(values); //this is the return value that causes the error

Some insight onto why it works with console.WriteLine but not return would be great and how to fix it so it works with return would be great.

CodePudding user response:

Don't do this:

return sum(values);

Your code isn't in a method, it's a relatively new C# feature called Top-level Statements. When you return from top-level statements, you're producing an "exit code" for the application. And...

  1. Exit codes are integers (hence the error, you're returning a double)
  2. Exit codes have a meaning (you don't want to return just any random value)

If you want to output the value, then output it instead of returning it from the application:

Console.WriteLine(values);

why it works with console.WriteLine but not return

Because Console.WriteLine is a method which accepts a variety of types. return is a statement which returns a value from a method.

In this case that "method" is effectively an implicit Main() method for the application, which has a return type of int.

CodePudding user response:

Good day friend Possibly your mistake is in the way you do your sum. I would recommend that you do it as follows:

  1. They start your arrangement directly as a list and convert your list into statica because your method is static the same.

static List<double> Numbers = new List{ -1.103f, 2.2f, 3.1f, 10.0f, 15.0f, 23.1f, 22, 12f };*

Note: Thus avoiding the unnecessary creation of the array, unless it is necessary to start the object in array and you need to make validations before adding them to the list if so, your process is good, otherwise I recommend doing it this way.

  1. Use linq expressions(using System.Linq;) for your sum with the following

static void SumList() => Console.WriteLine(Numbers.Sum().ToString("N0"));

Note: We generated a method called SumList that will return the sum of the list since in your code you use a static method places it like this.

Your code would end as follows

static List<double> Numbers = new List<double> { -1.103f, 2.2f, 3.1f, 10.0f, 15.0f, 23.1f, 22, 12f };

static void Suma() => Console.WriteLine(Numbers.Sum().ToString("N0"));

PS: I hope you will excuse me for my English, and help you by example.

  • Related