Returning an ArrayList is as bad or worse as returning an object from most methods, so don’t. When you return an ArrayList, anything could be in there, so I have to try and read your code, or hope your documentation is still correct, to find out what’s in there. A much nicer and more type-safe solution is to return an Array of the objects in your ArrayList, which is only 1 line of code more.
public ArrayList BadNumbers{
get{
return this.items;
}
}
public int[] GoodNumbers{
get{
if( numbers == null ){
// it's not nice to return a null reference, when
// an Array is expected.
// That would mess up your foreach loop badly.
return new int[0];
}
// conversion is done here instead of at the call site
return (int[])numbers.ToArray(typeof(int));
}
}
private Arraylist numbers = new ArrayList();