I am trying to get familiar with C# and tried out the following program - it just outputs the average of the even numbers in the Array.
You need select.Average()
(with the parens).
This was a careless mistake on my part, I was trying to call Method like a property instead of Method() like a method
You are not calling Average
. should be select.Average()
The Missing Parenthesis ()
is the reason for your error.It should be Average()
without a Parenthesis,it is understood as a method group.The average method could have multiple overloads and it is unclear which specific overloaded method needs to be invoked.But when you mention the parenthesis it makes the intention clearer and the method gets called.
It's an Extension Method
so it should be like this: Average()
with ( Parenthesis() )
the problem is that, you forgot to include the parenthesis since Average
is a method (extension type). Another solution is to use lambda expression, something like this,
var numbers = new[] { 1, 2, 3, 4, 5 };
Console.WriteLine(numbers.Where(x => (x % 2) == 0).Average());
or
var numbers = new[] { 1, 2, 3, 4, 5 };
var select = (from num in numbers where (num % 2) == 0 select num).Average();
Console.WriteLine(select);