StackOverflow while setting ListView.ItemsSource
So I have a simple struct Point
with two doubles X
and Y
. I calculate an array of about three hundred of them and set that array as ItemsSource for a ListView in WPF. That call eventually throws a StackOverflowException
.
De debugger breaks at the beginning of the Equals
method in my struct, which I implemented like so (should it matter):
public override bool Equals(object obj)
{
开发者_JS百科 if (obj is Point)
return Equals(obj);
return false;
}
public bool Equals(Point other) // Implement IEquatable<T>
{
return this.x == other.x && this.y == other.y;
}
If I change that to this:
public override bool Equals(object obj)
{
return false;
}
Nothing happens and the numbers get displayed. I really don't know what I did wrong here, so I don't know how to fix this. Any pointers?
The program is trying to call Equals(object obj)
again because you are passing obj
as an object
even though it's a Point
. So essentially that overload is calling itself again and again.
You have to cast obj
to Point
when you pass it in the inner call, so it'll call the Equals(Point other)
method instead:
public override bool Equals(object obj)
{
if (obj is Point)
return Equals((Point) obj);
return false;
}
Just a quickie - an alternative way of writing the Equals(object) method is:
public override bool Equals(object obj)
{
return (obj is Point) && Equals((Point)obj);
}
(The first set of brackets isn't actually necessary, but I think it helps the readability.)
精彩评论