Here's an off-the-wall idea:
Is Accounts declared as List<Account>
?
I'm wondering if Accounts
is a property declared as something other than List<Account>
-- for instance, as IList<Account>
-- and you have a static helper class somewhere with a Sort
extension method that isn't implemented properly. This could attempt to leverage the List<T>.Sort
method when the parameter passed in is a List<T>
, but do so without performing the necessary cast to List<T>
, resulting in a guaranteed StackOverflowException
.
What I mean is this. Suppose Account
is a property of some class that looks something like this:
public class AccountManager
{
public IList<Account> Accounts { get; private set; }
public AccountManager()
{
// here in the constructor, Accounts is SET to a List<Account>;
// however, code that interacts with the Accounts property will
// only know that it's interacting with something that implements
// IList<Account>
Accounts = new List<Account>();
}
}
And then suppose elsewhere you have this static class with a Sort
extension method:
public static class ListHelper
{
public static void Sort<T>(this IList<T> list, Comparison<T> comparison)
{
// programmer tries to use the built-in sort, if possible
if (list is List<T>)
{
// only problem is, list is here still typed as IList<T>,
// so this line will result in infinite recursion
list.Sort(comparison);
// the CORRECT way to have done this would've been:
// ((List<T>)list).Sort(comparison);
return;
}
else
{
list.CustomSort(comparison);
return;
}
}
private static void CustomSort<T>(this IList<T> list, Comparison<T> comparison)
{
// some custom implementation
}
}
In this case, the code you posted would throw a StackOverflowException
.
Original answer:
Perhaps Accounts
is an object of a custom collection class whose Sort
method calls itself?
public class AccountCollection : IEnumerable<Account> {
// ...
public void Sort(Comparison<Account> comparison) {
Sort(comparison); // infinite recursion
}
// ...
}
Perhaps the AccountId
property calls itself?
public class Account {
// ...
public string AccountId {
get { return AccountId; } // infinite recursion
}
// ...
}