Câu hỏi phỏng vấn C#
Câu hỏi

Can you create a function in C# which ...

Câu trả lời

Yes, you can create a function in C# that can accept a varying number of arguments using the params keyword. Here's an example:

csharp Copy
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:

csharp Copy
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

senior

Gợi ý câu hỏi phỏng vấn

junior

What are Reference Types in C#?

expert

What is jagged array in C# and when to prefer jagged arrays over multi-dimensional arrays?

junior

Can multiple catch blocks be executed?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào