return the sum of the numbers in the parameter list.

titotitus

Member
Joined
Feb 12, 2020
Messages
14
Programming Experience
Beginner
C#:
 public static void Main(string[] args)
    {
      int result = Sum();
    }


   public static int Sum(List<int> numbers)  
  {
       //List<int> numbers = new List<int>();  
            numbers.Add(3);  
            numbers.Add(2);  
            numbers.Add(6);  
            numbers.Add(-1);  
            numbers.Add(5);  
            numbers.Add(1);  
            return numbers.Sum();  
  }
 
  }
}

what im i doing wrong?
the error is


Program.cs(10,20): error CS7036: There is no argument given that corresponds to the required formal parameter 'numbers' of 'Program.Sum(List<int>)' [C:\Users\mngar\Desktop\school\coding-exercises\part3\lists\exercise_76\exercise_76.csproj]
Program.cs(23,28): error CS1061: 'List<int>' does not contain a definition for 'Sum' and no accessible extension method 'Sum' accepting a first argument of type 'List<int>' could be found (are you missing a using directive or an assembly reference?) [C:\Users\mngar\Desktop\school\coding-exercises\part3\lists\exercise_76\exercise_76.csproj]

The build failed. Fix the build errors and run again.
 
Last edited:
Well, there's two issues, but both of them are clearly outlined by the error messages.

The first error is that your Sum() method requires a list of integers as a parameter, but you are not passing any parameters when you make that call:
C#:
int result = Sum();

The second error is because you are expecting the method Sum() to be supported by the list data structure. It doesn't have a such a method. You may have seen code floating around on line where people call Sum() or Average() on a list and it seems to work. That's because they are using System.Linq; which introduces a bunch of extension methods like Sum() and Average() to anything that offers an IEnumerable<T> interface.

To progress as a progammer, you are going to have to take time to learn how to read the error messages. You can't just throw your hands up in the air every time your code fails to compile.
 
Also, look closely at your Exercise #76. The code presented there is what is supposed to be in your Main(). Notice have numbers are added to the list, and the list is passed in to Sum()? So all you really need to do is implement the body of the Sum() method.
 
Back
Top Bottom