473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why do I need "new" when implementing CollectionBase.RemoveAt()???

I've implemented a number of strongly-typed collections that inherit from
CollectionBase and recently noticed something that I don't fully understand.

CollectionBase defines method RemoveAt(). However, CollectionBase
implements IList which also defines method RemoveAt(). In my collection when
I code my own RemoveAt() method, the compiler issued a warning indicating
that my RemoveAt() must be defined with a "new" keyword. So, to keep the
compiler happy, I added the new keyword as follows:

new public virtual void RemoveAt( int idx )
{
List.RemoveAt( idx );
}

I'm guessing that this invokes the IList.RemoveAt() method since I'm
explicitly indicating List.RemoveAt(). If I code it as follows, I assume it
invokes the CollectionBase.RemoveAt() method:

new public virtual void RemoveAt( int idx )
{
RemoveAt( idx );
}

The new keyword as a modifier hides the base classe's implementation but
don't these two methods do the same thing? Why does CollectionBase's
implementation of RemoveAt() hide the IList implementation? Why doesn't it
just use IList's? Or am I missing something else??? :)

..ARN.
Jul 22 '05 #1
1 2068
alanrn <al****@discussions.microsoft.com> wrote:
I've implemented a number of strongly-typed collections that inherit from
CollectionBase and recently noticed something that I don't fully understand.

CollectionBase defines method RemoveAt(). However, CollectionBase
implements IList which also defines method RemoveAt(). In my collection when
I code my own RemoveAt() method, the compiler issued a warning indicating
that my RemoveAt() must be defined with a "new" keyword. So, to keep the
compiler happy, I added the new keyword as follows:
<snip>

This is because RemoveAt isn't a virtual method. That means that if
anyone has a reference to your collection just as a CollectionBase and
calls RemoveAt, it won't call your method - it will call
CollectionBase's implementation.
new public virtual void RemoveAt( int idx )
{
List.RemoveAt( idx );
}

I'm guessing that this invokes the IList.RemoveAt() method since I'm
explicitly indicating List.RemoveAt().


No, that's calling RemoveAt on the IList which the List property
returns - that's not the same as calling:

((IList)this).RemoveAt(idx)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there...
9
by: James | last post by:
Hi, I am new to C++. I want to directly create a dynamic two-dimension double array, i.e. double pp. I found the "new" is only for one-dimension array, i.e. double *p = new p. How to "new" a...
24
by: Rv5 | last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little. I have...
14
by: Atara | last post by:
I know in C++ it is true. but what about VB .Net and its GarbageCollector ? For example, consider the following case. Does f2() needs to do any disposing code ? Public Sub f1() As...
1
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). ...
51
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and...
4
by: Ronald S. Cook | last post by:
So most of the time I need to write the following to instantiate an object: Dim cnn As New SqlConnection() But sometimes Intellisense tells me not to include the "new": Dim dst As DataSet ...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
12
by: Jordi | last post by:
I'm getting the following error: Software error: Can't locate object method "new" via package "A::B" at /path/file.cgi line 5. My code is basically this: #!/usr/bin/perl -w use strict; use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.