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

middle

What is the use of Null Coalescing Operator ( ?? ) in C#?

expert

What is deep or shallow copy concept in C#?

expert

What is a preprocessor directives in C#?

Bình luận

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

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