If it is with a repetition of the same type, that you work with, and those
are last parameters, your can also use the variable argument list way as in:
void foo (params int [] all)
{
int sum = 0 ;
foreach (int i in all) sum += i ;
Console.WriteLine ("{0}", sum) ;
}
called as : foo (); foo (1); foo (1,2); foo (1,2,3); etc...
/LM
Soli3d said:
Thanks for the push. I like this way.
Code:
class Tumble
{
static void Main(string[] args)
{
foo();
foo(1);
foo(1, 2);
foo(1, 2, 3);
}
static public void foo(int a, int b, int c)
{
int result = (a + b + c);
Console.WriteLine(result.ToString());
}
static void foo(int a, int b)
{
int c = 3;
foo(a, b, c);
}
static void foo(int a)
{
int b = 2;
foo(a, b);
}
static void foo()
{
int a = 1;
foo(a);
}
}
Cheers,
Paul
Jon Skeet said:
Is there a way to Inherit from a Method? I don't want to have to repeat
the code in my method over and over for the overloaded versions. If I
need
to make changes I don't want to have to reapeat them in each overloaded
version of my method.
Better way to do this?
The normal way is to make one overload which has all the information
you could possibly need, and call it from other methods, passing in
parameters. For instance:
public object LookupItem (object key, object defaultValue)
{
... do the lookup ...
}
// Same as LookupItem (key, defaultValue) but with a default value of
// null
public object LookupItem (object key)
{
return LookupItem (key, null);
}