Creating the tuple function
Start a new Visual C# console app and simplify the template back to our standard opening code:
using static System.Console; class Program { static void Main() { } }
Create a new function that will return a tuple. Underneath the opening curly brace of our Program
class, type the following:
static (double sum, double average) Summarize(double[] arr) { }
Here, I'm defining the return type of the function, which in this case is a tuple. We're returning two values here and defining each of their data types. That's the unique benefit of tuples: being able to return two values from a single function call. We're calling it Summarize
, and it will accept an argument called arr
of type double array
--so an array of doubles. It's going to return a tuple, which specifically means two values.
With this in place, let's define the body. The body of this function is pretty much the same as the one we used for our out
keyword example, but we need to do something...