Can you create a function in C# which ...
Can you create a function in C# which ...
Yes, you can create a function in C# that can accept a varying number of arguments using the params keyword. Here's an example:
public static int Sum(params int[] numbers)
{
int total = 0;
foreach (int number in numbers)
{
total += number;
}
return total;
}
In this example, the Sum
function uses the params
keyword to accept an array of integers as arguments. The function can be called with any number of integer arguments, and they will be treated as an array inside the function.
Here's how you can call the Sum
function with varying arguments:
int result1 = Sum(1, 2, 3); // result1 = 6
int result2 = Sum(4, 5); // result2 = 9
int result3 = Sum(); // result3 = 0
The params
keyword all...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào