Can you explain me this strange behaviour of c# with optional arguments? [closed]?

Up vote 15 down vote favorite 6 share g+ share fb share tw.

Here the code to try it yourself (VS2010 and FW 4): static void Main(string args) { Peter p = new Peter(); p.TellYourAge(); // expected -1, result: -1 p.DoSomething(); // expected -1, result: 0 Fred f = new Fred(); f. TellYourAge(1); // expected 1, result: 1 f.DoSomething(); // expected 1, result: 1 Console.ReadKey(); } } public abstract class Person { public abstract void TellYourAge(int age); // abstract method without default value } public class Peter : Person { public override void TellYourAge(int age = -1) // override with default value { Console. WriteLine("Peter: " + age); } public void DoSomething() { TellYourAge(); } } public class Fred : Person { public override void TellYourAge(int age) // override without default value { Console.

WriteLine("Fred: " + age); } public void DoSomething() { TellYourAge(1); } } c# optional-arguments link|improve this question edited Mar 5 at 15:24Christofer Eliasson5,2041519 asked Mar 5 at 15:22X181785.

2 My first guess is that since int is not nullable here: public override void TellYourAge(int age = -1) // override with default value, it's default is always 0... – mservidio Mar 5 at 15:26 2 @mservidio But that's why you specify the default value – Oskar Kjellin Mar 5 at 15:28 1 You might be interested in this article by Eric Lippert. Although it doesn't describe the exact same case, it should give you some clues about what's happening. – Thomas Levesque Mar 5 at 15:29 1 Crack open Resharper/ILSpy and see what has been compiled.

– Oded Mar 5 at 15:30 6 See stackoverflow.com/questions/8909811/… – Sean U Mar 5 at 15:30.

If you happen to use Resharper, it will give you the following warning / notification. "Optional parameter default value differs from parameter age in base method void TellYourAge(int age). " Look out when you mix optional parameter values and inheritance.

Default parameter values are resolved at compile time, not runtime. The default belongs to the reference type being called. Here it resolves to the Person type and it uses the default value of an integer which is 0, instead of -1.

You can find some information about common pitfalls regarding optional parameters here: geekswithblogs.net/BlackRabbitCoder/arch... Easy fix if you want to use it this way. Explicitly specify the keyword 'this' when calling the method TellYourAge. This way the desired default value will be determined at compile time.

Public void DoSomething() { this.TellYourAge(); }.

1 I would expect a compile error or warning in this case, but that's not the point. That the default parameters are resolved at compile time is also clear, but does not explain why the exactly same call to TellYourAge() on the p-instance results in different output. – X181 Mar 5 at 15:46 See also blogs.msdn.com/b/ericlippert/archive/201... – Eric Lippert Mar 5 at 15:48 1 The topics are related, but its not a duplicate.

In the other case a specified default value is used. In this case the unspecified default value of the type is used. – X181 Mar 5 at 16:08 @X181 I believe it's the very same compiler bug.

JonSkeet's answer to the old question even tested this case, where the base class has a mandatory parameter. – CodeInChaos Mar 7 at 17:07.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions