Collection interfaces

N

nooboy

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

Jon Skeet [C# MVP]

nooboy said:
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.
 
N

nooboy

Jon Skeet said:
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?
 
L

Liz

nooboy said:
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
 
N

nooboy

Liz said:
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?
 
P

Peter Duniho

[...]
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
 
P

Peter Duniho

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
 
L

Liz

nooboy said:
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.
 
N

nooboy

Liz said:
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.
 

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