Stack overflow calling from generic method to non-generic method. - DOTNET
This is a discussion on Stack overflow calling from generic method to non-generic method. - DOTNET ; In the following code (over-simplified, of course), the generic method calls
itself recursively, quickly resulting in a StackOverflowException:
public T[] GetArray<T>( T value )
{
return GetArray( value );
}
public Array GetArray( object value )
{
return return new ...
-
Stack overflow calling from generic method to non-generic method.
In the following code (over-simplified, of course), the generic method calls
itself recursively, quickly resulting in a StackOverflowException:
public T[] GetArray<T>( T value )
{
return GetArray( value );
}
public Array GetArray( object value )
{
return return new ArrayList().ToArray();
}
I assumed the second method would be called because the notation of the call
(lacking the 'generic' brackets) indicated the non-generic method would be
called. Why does this behave this way?
-
Re: Stack overflow calling from generic method to non-generic method.
<=?Utf-8?B?RWQgQ2hhcGVs?= <Ed Chapel@discussions.microsoft.com>>
wrote:
> In the following code (over-simplified, of course), the generic method calls
> itself recursively, quickly resulting in a StackOverflowException:
>
> public T[] GetArray<T>( T value )
> {
> return GetArray( value );
> }
>
> public Array GetArray( object value )
> {
> return return new ArrayList().ToArray();
> }
>
> I assumed the second method would be called because the notation of the call
> (lacking the 'generic' brackets) indicated the non-generic method would be
> called. Why does this behave this way?
It's using type inference to call the generic version without
explicitly stating the type involved. T is more specific than object,
hence the compiler is choosing the generic version over the non-generic
version.
If you cast value to object, your call will go to the non-generic
version.
You'd have to look at the specs in detail for the exact steps the
compiler will take.
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
-
Re: Stack overflow calling from generic method to non-generic meth
That answers my question. Thank you! I learn something new everyday.
Ed
"Jon Skeet [C# MVP]" wrote:
> <=?Utf-8?B?RWQgQ2hhcGVs?= <Ed Chapel@discussions.microsoft.com>>
> wrote:
> > In the following code (over-simplified, of course), the generic method calls
> > itself recursively, quickly resulting in a StackOverflowException:
> >
> > public T[] GetArray<T>( T value )
> > {
> > return GetArray( value );
> > }
> >
> > public Array GetArray( object value )
> > {
> > return return new ArrayList().ToArray();
> > }
> >
> > I assumed the second method would be called because the notation of the call
> > (lacking the 'generic' brackets) indicated the non-generic method would be
> > called. Why does this behave this way?
>
> It's using type inference to call the generic version without
> explicitly stating the type involved. T is more specific than object,
> hence the compiler is choosing the generic version over the non-generic
> version.
>
> If you cast value to object, your call will go to the non-generic
> version.
>
> You'd have to look at the specs in detail for the exact steps the
> compiler will take.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>
Similar Threads
-
By Application Development in forum DOTNET
Replies: 3
Last Post: 11-14-2007, 04:47 PM
-
By Application Development in forum CSharp
Replies: 12
Last Post: 10-10-2007, 11:44 AM
-
By Application Development in forum CSharp
Replies: 1
Last Post: 08-06-2007, 11:54 AM
-
By Application Development in forum CSharp
Replies: 3
Last Post: 08-03-2007, 09:53 AM
-
By Application Development in forum DOTNET
Replies: 0
Last Post: 07-19-2007, 06:14 AM