2
Answers

Boxing Mystery in Overloaded and Overrided Method

Ask a question
angwin

angwin

16y
1.9k
1
Hello!

I have a class with two overloaded methods

int Foo(int i) { Foo(i); }
int Foo(int? i) { }

First method call leads to StackOverflowException. It is OK.

Change the code so that the base class contains virtual method "int Foo(int i)" and inhereted class declares

override int Foo(int i) { Foo(i); }
int Foo(int? i) { }

After calling the first method, a mystery occurs - the value-type argument "i" is boxed and the second method is called instead of repeating the first one. Why? Articel with complete source code and its IL-code is here: http://dotnet-enthusiast.blogspot.com/2007/09/boxing-mystery-in-overloaded-and.html

Answers (2)