ArrayList adapter problem

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?
 
W

William Stacey [MVP]

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?
 
P

Paul Rudin

"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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top