I’m trying to write a Set class where it’s a list that you can add an element only once. So I naturally tried to extend the ArrayList and wrote this code. The problem is that it compiles but during execution it gives this error and points to the “if (!(this.Contains(value)))” line :
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
Here is my program:
public class Set : System.Collections.ArrayList
{
public Set()
{
}
public override int Add(object value)
{
int res;
if (!(this.Contains(value)))
{
res = this.Add(value);
}
else
{
res = this.IndexOf(value);
}
return res;
}
}