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:

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

senior

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

expert

What is the volatile keyword used for?

middle

Explain the difference between Task and Thread in .NET

expert

in C#, when should we use abstract classes instead of interfaces with extension methods?

Bình luận

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

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