Sagar
Sagar

Reputation: 7605

How to pass different numbers of parameters in a same function?

I got similar questions of this, but not get satisfied answer.

I have a function

func(int a,int b)
{
//code....
}

This function accepts two parameters (a and b). I want to pass different number of parameters in this same function. I know there is a concept of overloading but I don't know how many numbers of parameters I'll pass. I am working in C#(asp.net).

Upvotes: 0

Views: 652

Answers (1)

aleroot
aleroot

Reputation: 72676

You could use varargs(params in c#) ...

Example in C# :

public void func(params int[] numbers)
        { 
            for(int i = 0; i < numbers.Length; i++) 
            { 
                Console.WriteLine(numbers[i]);
            } 
        }

Upvotes: 6

Related Questions