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 ...

+ Reply to Thread
Results 1 to 3 of 3

Stack overflow calling from generic method to non-generic method.

  1. Default 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?

  2. Default 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

  3. Default 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
    >


+ Reply to Thread

Similar Threads

  1. Visual Studio non-generic method bug
    By Application Development in forum DOTNET
    Replies: 3
    Last Post: 11-14-2007, 04:47 PM
  2. Generic Method Help
    By Application Development in forum CSharp
    Replies: 12
    Last Post: 10-10-2007, 11:44 AM
  3. Using ForEach method of Generic List Object.
    By Application Development in forum CSharp
    Replies: 1
    Last Post: 08-06-2007, 11:54 AM
  4. Generic Method return Type
    By Application Development in forum CSharp
    Replies: 3
    Last Post: 08-03-2007, 09:53 AM
  5. could not find a non-generic method
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 07-19-2007, 06:14 AM