live wid knowledge :)

function with n number of arguments

rarely we could need of such function but i m sure whilst programming, programmers have used such functions of their framework functions/base library functions e.g, in case of c language you must have used printf() function (if u didn’t use it i can say u r n’t c programmer :D) it takes n number of arguments, hmm still confuse ok let me tell u how ………..

printf(”sum of %d and %d is %d”,1,2,1+2) ; i hope now you got my point.

but what if you have to make function with n number of arguments using .net (c#.net) then don’t worry main hoon na :D simply u would need to know/utilize PARAMS keyword of c# let me give u any example

private object Sum(params object[] numbers)
    {
        int sum = 0;
        foreach (object o in numbers)
            if (o.GetType() == typeof(int))
                sum += (int)o;
        return (object)sum;
    }

i used object to make it more flexible for you.

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)