473,401 Members | 2,068 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,401 software developers and data experts.

Collection interfaces

Confused about this concept. If I define some collection class and inherit
IList, I get an error if I don't implement the interface methods. This
makes sense.

But if I inherit from CollectionBase, I don't get this error, yet the
methods still don't seem to be implemented. For example, this code gives a
compile error.

class TestColl : IList
{
}

because, for one, it doesn't implement Add(...)

But this one does not give an error, even though the source for
CollectionBase shows that it does not implement Add(...) either.

class TestColl : CollectionBase, IList
{
}

What concept am I missing here?
Dec 19 '07 #1
8 1405
nooboy <no****@nospam.comwrote:
Confused about this concept. If I define some collection class and inherit
IList, I get an error if I don't implement the interface methods. This
makes sense.

But if I inherit from CollectionBase, I don't get this error, yet the
methods still don't seem to be implemented. For example, this code gives a
compile error.
They're implemented by CollectionBase, but explicitly. In other words,
if you cast a CollectionBase to IList, you can use them.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 19 '07 #2

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .
nooboy <no****@nospam.comwrote:
>Confused about this concept. If I define some collection class and
inherit
IList, I get an error if I don't implement the interface methods. This
makes sense.

But if I inherit from CollectionBase, I don't get this error, yet the
methods still don't seem to be implemented. For example, this code gives
a
compile error.

They're implemented by CollectionBase, but explicitly. In other words,
if you cast a CollectionBase to IList, you can use them.
Did you mean to say "explicitly"? In any case, let's take Add() as an
example. Where is it actually implemented?
Dec 19 '07 #3
Liz

"nooboy" <no****@nospam.comwrote in message
news:OB**************@TK2MSFTNGP03.phx.gbl...
Confused about this concept. If I define some collection class and
inherit IList, I get an error if I don't implement the interface methods.
This makes sense.

But if I inherit from CollectionBase, I don't get this error, yet the
methods still don't seem to be implemented. For example, this code gives
a compile error.

class TestColl : IList
{
}

because, for one, it doesn't implement Add(...)

But this one does not give an error, even though the source for
CollectionBase shows that it does not implement Add(...) either.
sure it does ... did you look at the "Explicit Interface Implementations" in
the docs? you'll see System.Collections.IList.Add

>
class TestColl : CollectionBase, IList
{
}

What concept am I missing here?

Dec 19 '07 #4

"Liz" <li*@tiredofspam.comwrote in message
news:un**************@TK2MSFTNGP04.phx.gbl...
>
>>
But this one does not give an error, even though the source for
CollectionBase shows that it does not implement Add(...) either.

sure it does ... did you look at the "Explicit Interface Implementations"
in the docs? you'll see System.Collections.IList.Add
I see. Thank you. I assume that's what Jon meant when he said
"explicitly". Can you tell me where it is in the source? i.e. when I click
on CollectionBase in my source code and "Go To Definition", the source code
doesn't show it. Is there an easy way to get to it?
Dec 19 '07 #5
On Wed, 19 Dec 2007 10:41:46 -0800, nooboy <no****@nospam.comwrote:
[...]
>>But if I inherit from CollectionBase, I don't get this error, yet the
methods still don't seem to be implemented. For example, this code
gives
a compile error.

They're implemented by CollectionBase, but explicitly. In other words,
if you cast a CollectionBase to IList, you can use them.

Did you mean to say "explicitly"? In any case, let's take Add() as an
example. Where is it actually implemented?
Yes, that's what he meant.

Read the docs on interfaces in C#. Particularly the difference between an
implicit and explicit implementation. With the latter, you can only use
the interface member through the interface itself (e.g. by casting the
instance to that interface).

It's implemented by CollectionBase. But because it's an explicit
implementation, you have to cast your CollectionBase instance to IList
first before you can use it.

Pete
Dec 19 '07 #6
On Wed, 19 Dec 2007 11:08:13 -0800, nooboy <no****@nospam.comwrote:
I see. Thank you. I assume that's what Jon meant when he said
"explicitly". Can you tell me where it is in the source? i.e. when I
click
on CollectionBase in my source code and "Go To Definition", the source
code
doesn't show it. Is there an easy way to get to it?
CollectionBase is implemented the same place all the other .NET classes
are. In the .NET framework itself.

Microsoft has announced they will be making the .NET source code available
for debugging purposes, but until that's actually happened and you've
configured your Visual Studio to be able to find the source code, you
won't be able to see it.

If you want, for the moment AFAIK you can use Reflector to look at the
implementation. But Visual Studio won't be able to show it to you.

Pete
Dec 19 '07 #7
Liz

"nooboy" <no****@nospam.comwrote in message
news:O9*************@TK2MSFTNGP03.phx.gbl...
>
"Liz" <li*@tiredofspam.comwrote in message
news:un**************@TK2MSFTNGP04.phx.gbl...
>>
>>>
But this one does not give an error, even though the source for
CollectionBase shows that it does not implement Add(...) either.

sure it does ... did you look at the "Explicit Interface Implementations"
in the docs? you'll see System.Collections.IList.Add

I see. Thank you. I assume that's what Jon meant when he said
"explicitly". Can you tell me where it is in the source? i.e. when I
click on CollectionBase in my source code and "Go To Definition", the
source code doesn't show it. Is there an easy way to get to it?

what Peter says is correct, but consider:

class LCollection : CollectionBase
{

}

private void myMethod()
{
IList l = new LCollection();
l.Add("some string value");
}

if you highlight "Add" and right-click into "Go To Definition" VS *will*
show you the IList interface definition and comments from the metadata ...
but not the source code for the Add method

Notice that IList also combines ICollection and IEnumerable, so all members
of those interfaces are implemented by implementers of IList.

Dec 19 '07 #8

"Liz" <li*@tiredofspam.comwrote in message
news:eG**************@TK2MSFTNGP06.phx.gbl...
>

what Peter says is correct, but consider:

class LCollection : CollectionBase
{

}

private void myMethod()
{
IList l = new LCollection();
l.Add("some string value");
}

if you highlight "Add" and right-click into "Go To Definition" VS *will*
show you the IList interface definition and comments from the metadata ...
but not the source code for the Add method
Right, exactly, that was my question. It's confusing to be taken to source
code, but not being able to see the actual implementation. It's a little
frustrating because it looks like they're showing you everything, but
they're not telling you which parts they're not showing.
Dec 19 '07 #9

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

Similar topics

2
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
4
by: Peter | last post by:
Hello Thanks for reviewing my question. I believe I read that a DataGrid can point to a collection but would like to know if you can point to an example. How does the DataGrid know what are the...
4
by: Peter | last post by:
Hello Thanks for reviewing my question. I believe I read that a DataGrid can point to a collection but would like to know if you can point to an example. How does the DataGrid know what are the...
5
by: Peter | last post by:
Hello Thanks for reviewing my question. I believe I read that a DataGrid can point to a collection but would like to know if you can point to an example. How does the DataGrid know what are the...
2
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.