Amazing!!! ListViewItemCollection interfaces

  • Thread starter Thread starter Aleksey
  • Start date Start date
A

Aleksey

Hi, guys!
I'm trying to write collection and in example to do this I observed
ListViewItemCollection public methods and properties. I was impressed when I
find that the methods definitions in ListViewItemCollection different from
methods in interfaces it implements!!!
For example:
IList methods:
int Add(object value);
void Insert(int index, object value);
ListViewItemCollection methods:
public virtual ListViewItem Add(ListViewItem value);
public ListViewItem Insert(int index, ListViewItem
item);

There is no method Add in ListViewItemCollection returns int!!!
May be I'm not qualified enough, but I don't understand how could they
change interface signature!!! It's not about the methods params, becuase I
suppose that they possibly use IList<>, so the parameter may be changed from
Object to ListViewItem... It's about the return value type!!!!
Your opinions... and any way you know to do such a things...

Best regards
Aleksey
 
Aleksey said:
Hi, guys!
I'm trying to write collection and in example to do this I observed
ListViewItemCollection public methods and properties. I was impressed when I
find that the methods definitions in ListViewItemCollection different from
methods in interfaces it implements!!!
For example:
IList methods:
int Add(object value);
void Insert(int index, object value);
ListViewItemCollection methods:
public virtual ListViewItem Add(ListViewItem value);
public ListViewItem Insert(int index, ListViewItem
item);

There is no method Add in ListViewItemCollection returns int!!!

ListViewItemCollection implements IList through explicit interface
implementation. You can get at its method int IList.Add(object) by
casting to an IList (although the docs suggest you shouldn't, for this
object).

"Explicit Interface Implementation (C# Programming Guide)"
<http://msdn2.microsoft.com/en-us/library/ms173157(VS.80).aspx>
 
I still don't understand how could it be, that when I write
listView1.Items.Add all overloaded Add methods return ListViewItem instead
of int (index) ?!?!
 
Aleksey said:
I still don't understand how could it be, that when I write
listView1.Items.Add all overloaded Add methods return ListViewItem instead
of int (index) ?!?!

Yes, but if you do this:

ListViewItemCollection coll = this.listView1.Items;
IList list = coll;

then when you type "list." you will see the other Add method.
 
Hi,

I believe its that "Explicit Interface Implementation" thing. Like
this:

class MyCollection : IList
{
public int Add(MyObject value)
{
...
}

// This can only be accessed using an variable of type IList
ListViewItem IList.Add(object value) // No 'public' and its
'IList'.Add
{
return this.Add((MyObject)value);
}
}

Hope that helped,

Regards
Raje
 
Back
Top