ArrayList adapter problem

  • Thread starter Thread starter Paul Rudin
  • Start date Start date
P

Paul Rudin

Here's some c# code:

using System;
using System.Collections;
namespace AdapterProblem
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
ArrayList source = ArrayList.Adapter(new object [] {"1" , "2"});
ArrayList range = source.GetRange(1,1);
ArrayList target = new ArrayList();
target.AddRange(range);
Console.WriteLine(target[0] == null? "It's null" : target[0]);
Console.ReadLine();
}
}
}

I would expect this to print out the second element of source -
i.e. the string "2". Actually it turns out that target[0] is null so
the program prints "It's null".

Is this a bug? If this behaviour is as it should be could someone
please explain why?
 
It seems. As the range is a "view" into source, using the range as
ICollection for AddRange must have something to do with this. Not sure if
that usage of a Range is supported or not?
 
"WS" == William Stacey [MVP] <William> writes:

WS> It seems. As the range is a "view" into source, using the range
WS> as ICollection for AddRange must have something to do with this.
WS> Not sure if that usage of a Range is supported or not?

Thanks for your reply. How would one know whether it's "supported" or
not? GetRange returns an ArrayList which supports the ICollection
interface... AddRange shouldn't presumably be relying on anything
else. However the problem does appear to be something to do
ArrayList.Adapter since it doesn't arise without this.

The slightly strange thing is that other obvious tests of the result
of the call to GetRange seem to give sensible results...
 
Back
Top