could someone remind me the rules of params.

is it... the params method needs to be the last one and you can have only params as the parameter in the method and nothing else?

Recommended Answers

All 3 Replies

params must be last, and only one params statement in a method. You can have any number of other parameters to the method as you want, they just must be before the params statement

class Program
    {
        static void Main(string[] args)
        {
            Foo(2, 5, 4, 3, 8, 7, 6);
            Foo(new int[] { 2, 5, 6, 7, 4, 3, 3, });

            Foo1(new int[] { 2, 5, 6, 7, 4, 3, 3, });
            Foo2("dor", 2.9, 5, 5, 4, 2, 5, 2, 5);
        }
        static void Foo(params int[] x)
        {
            for (int i = 0; i < x.Length; i++)
            {
                Console.WriteLine(x[i]);
            }
        }
        static void Foo1(int[] x)
        {

        }
        static void Foo2(string s, double d, int i, params int[] x)
        {
            Console.WriteLine("I = "+i);
        }
         //static void Foo2(params int[] x ,string s){

         //}
    }]

is the last method legit?

Yes it is legit and, unless they fixed it, you can do some funny things with the call. But don't! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.