
When I read Scott Guthrie on default parameters I remembered a test I wrote a little while back:
interface IA
{void F(int i = 3);}class A : IA
{public virtual void F(int i = 4){Console.WriteLine("Impl A: "+i);
}}class B : A
{public override void F(int i = 5){Console.WriteLine("Impl B: " + i);
}}class Program
{static void Main(string[] args){IA ia = new B();
ia.F();A a = new B();
a.F();B b = new B();
b.F();}}What is the output? Do you still like default?
It is easy to understand what is going on when you know that defaults are solved at compile time and the default value is hard coded/copied into the method call. At compile time there is no knowledge of run time types…
Share this
3 comments
Default parameters is a cool feature, but it’s meant for COM interop. An entirely different scenario than what you’re doing here 😉
Willemm
I know that this feature is very cool for COM interop but it is a language feature that can save you a lot of redundant typing of overloaded versions of a method. And I was just being curious.
Erno de Weerd
They are meant for COM interop, but that doesn’t mean that people won’t use them as an alternative for overloads. It’s good to point out the tricky parts of default parameters, so that people will only use them as they are intended to be used.
Alex van Beek