ArrayList(ICollection) constructor & overriden ArrayList.AddRange().

S

Sylvain

Hi,

I'm encountering a very simple issue with ArrayList constructor
and AddRange() method overriding.

I'm defining a class that extends ArrayList and contains one
overriden method: AddRange(ICollection).

public class Test:ArrayList
{
public Test(Array array): base(array)
{
}

public override void AddRange(ICollection c)
{
}
}

If I create an instance of Test:
Test t = new Test(new int[]{1,2,3});

I get
t.Count = 0 instead of t.Count = 3 (what I was naively expecting)

It looks like the ArrayList(ICollection) constructor invoke the
virtual method AddRange().

1/ I was wondering if this is the desired behavior ?

-- and if yes, do you have a pointer for the coding rules of
constructors versus virtual method calls usage ?

2 / Should'nt this specified in the ArrayList documentation ?

Thanks in advance for your help.
Sylvain
 
J

Jonathan Allen

1/ I was wondering if this is the desired behavior ?

Seems logical to me. It gives you a good hook point to monitor objects that
are added.
2 / Should'nt this specified in the ArrayList documentation ?

That would be too hard to keep up to date in the general case. It would be
better to just assume that any function may call any other function (now or
in the future), and to design your class accordingly. One really should
override a method without supporting the expected underlying behavior for
many reasons.

--
Jonathan Allen


Sylvain said:
Hi,

I'm encountering a very simple issue with ArrayList constructor
and AddRange() method overriding.

I'm defining a class that extends ArrayList and contains one
overriden method: AddRange(ICollection).

public class Test:ArrayList
{
public Test(Array array): base(array)
{
}

public override void AddRange(ICollection c)
{
}
}

If I create an instance of Test:
Test t = new Test(new int[]{1,2,3});

I get
t.Count = 0 instead of t.Count = 3 (what I was naively expecting)

It looks like the ArrayList(ICollection) constructor invoke the
virtual method AddRange().

1/ I was wondering if this is the desired behavior ?

-- and if yes, do you have a pointer for the coding rules of
constructors versus virtual method calls usage ?

2 / Should'nt this specified in the ArrayList documentation ?

Thanks in advance for your help.
Sylvain
 

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