Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4551886
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:41:55+00:00 2026-05-21T16:41:55+00:00

after migrating from .NET 3.5 to 4.0 i have a strange problem with a

  • 0

after migrating from .NET 3.5 to 4.0 i have a strange problem with a function resulting in stack overflow on 4.0 while working perfectly on the 3.5 framework. I reproduced the problem with the following code:

public interface IPerson
{
    string Name { get; }
}

public class Person : IPerson
{
    public Person() { }

    public Person(IPerson source)
    {
        this.Name = source.Name;
    }

    public string Name { get; set; }
}

public class PersonList : List<Person>
{
    public void AddRange(IEnumerable<IPerson> source)
    {
        this.AddRange(source.Select(p => new Person(p)));
    }               
}

Provoking the error:

  IPerson otto = new Person { Name = "Otto" };
  IPerson fritz = new Person { Name = "Fritz" };

  PersonList list = new PersonList();

  IEnumerable<IPerson> persons = new[] { otto, fritz };
  list.AddRange(persons); //works on 3.5, stack overflow on 4.0

Have a look at the AddRange(IEnumerable<IPerson> source) method of PersonList.
In 3.5, the method AddRange(IEnumerable<Person> source) is called, derived from List<Person>. In 4.0 the AddRange(IEnumerable<IPerson> source) method is called (recursion) due to covariance, in spite the fact that a better matching function with a parameter (IEnumerable<Person>I) exactly matching the input parameter exists.

Is that new behaviour intended and documented?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:41:56+00:00Added an answer on May 21, 2026 at 4:41 pm

    This is proper C# behavior because in C# if any method on a more-derived class is an applicable candidate, it is automatically better than any method on a less-derived class, even if the less-derived method has a better signature match. So C# 4 has made AddRange(IEnumerable<IPerson> source) an applicable candidate and then the better signature AddRange(IEnumerable<Person> source) is in the base class so it doesn’t get picked.

    But it’s easily fixable in your case, because of the rule.

    public class PersonList  : List<Person>
    {
        public void AddRange(IEnumerable<IPerson> source)
        {
               base.AddRange(source.Select(p => new Person(p)));
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm migrating an application from ColdFusion to ASP.Net MVC and have a little problem
I have a large-ish web project which is migrating from classic ASP to ASP.NET
Came across something strange while migrating to my new server. I have a script
We are migrating from asp classic to .net. Unfortunately they named all of the
After migrating JSF from 1.2 to 2.0 I begin to receive exception after submitting
I'm using PDO after migrating away from the mysql library. What do I use
I'm working on a new Django site, and, after migrating in a pile of
We are not able to create the installer after migrating the code from VS2005
I am migrating 2 .NET web sites from one Windows 2003 (IIS6) server to
I have been migrating from subversion to mercurial piecemeal and this has created a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.