Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ed Chapel - 28 Mar 2006 02:05 GMT
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?
Jon Skeet [C# MVP] - 28 Mar 2006 06:24 GMT
<=?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:
[quoted text clipped - 12 lines]
> (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.

Signature

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

Ed Chapel - 28 Mar 2006 19:10 GMT
That answers my question. Thank you! I learn something new everyday.

Ed

>  <=?Utf-8?B?RWQgQ2hhcGVs?= <Ed Chapel@discussions.microsoft.com>>
> wrote:
[quoted text clipped - 25 lines]
> You'd have to look at the specs in detail for the exact steps the
> compiler will take.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.