473,490 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

another IList question

Hi again,
Ok, so the compiler tells me I need two GetEnumerator() methods
for my class implementing IList<string>: one returning an IEnumerator,
and the other returning IEnumerator<string>. Problem is, these methods
both take no parameters, so of course the compiler complains that it
can't tell the difference. But when I only implement one of them, the
compiler complains that it can't find the other one. How do I make the
compiler happy?

Thanks,
John

Aug 22 '06 #1
3 4510
jj******@gmail.com wrote:
Hi again,
Ok, so the compiler tells me I need two GetEnumerator() methods
for my class implementing IList<string>: one returning an IEnumerator,
and the other returning IEnumerator<string>. Problem is, these methods
both take no parameters, so of course the compiler complains that it
can't tell the difference. But when I only implement one of them, the
compiler complains that it can't find the other one. How do I make the
compiler happy?

Thanks,
John
Use explicit interface implementation:

class StringList : IList<string>
{
IEnumerator<stringIEnumerable<string>.GetEnumerato r()
{
// Do implementation here
throw new NotImplementedException();
}

IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<string>) this).GetEnumerator();
}
}
HTH,
Andy

--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
Aug 22 '06 #2
By the way.

If you use your class that implements the IList interface later within a
foreach loop - the non-generic Enumerator is called. If you need the
generic one you must cast:

sample:

class StringList : IList<string>
{
IEnumerator<stringIEnumerable<string>.GetEnumerato r()
{
// Do implementation here
throw new NotImplementedException();
}

IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<string>) this).GetEnumerator();
}
}

StringList myList ......

foreach(string str in myList)
{
// uses default enumerator
}

foreach(string str in (IEnumerable<string>)myList)
{
// uses generic enumerator
}

Matthias
Andreas Mueller schrieb:
jj******@gmail.com wrote:
>Hi again,
Ok, so the compiler tells me I need two GetEnumerator() methods
for my class implementing IList<string>: one returning an IEnumerator,
and the other returning IEnumerator<string>. Problem is, these methods
both take no parameters, so of course the compiler complains that it
can't tell the difference. But when I only implement one of them, the
compiler complains that it can't find the other one. How do I make the
compiler happy?

Thanks,
John
Use explicit interface implementation:

class StringList : IList<string>
{
IEnumerator<stringIEnumerable<string>.GetEnumerato r()
{
// Do implementation here
throw new NotImplementedException();
}

IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<string>) this).GetEnumerator();
}
}
HTH,
Andy
Aug 24 '06 #3
Matthias Heise wrote:
By the way.

If you use your class that implements the IList interface later within a
foreach loop - the non-generic Enumerator is called. If you need the
generic one you must cast:
Hi Matthias,

I dont't think that this is correct! The example below always uses the
generic version:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
StringList myList = new StringList();

foreach (string str in myList)
{
// uses generic enumerator!!
}

foreach (string str in (IEnumerable<string>)myList)
{
// uses generic enumerator!! cast not necessary
}
}
}
class StringList : IList<string>
{
IEnumerator<stringIEnumerable<string>.GetEnumerato r()
{
Debug.Assert(false, "IEnumerable<string>.GetEnumerator()");

// Dummy call to get sample running
return new List<string>().GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
Debug.Assert(false, "IEnumerable.GetEnumerator()");
// Dummy call to get sample running
return new List<string>().GetEnumerator();
}

public void Add(string item)
{
throw new NotImplementedException();
}

public void Clear()
{
throw new NotImplementedException();
}

public bool Contains(string item)
{
throw new NotImplementedException();
}

public void CopyTo(string[] array, int arrayIndex)
{
throw new NotImplementedException();
}

public bool Remove(string item)
{
throw new NotImplementedException();
}

public int Count
{
get { throw new NotImplementedException(); }
}

public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}

public int IndexOf(string item)
{
throw new NotImplementedException();
}

public void Insert(int index, string item)
{
throw new NotImplementedException();
}

public void RemoveAt(int index)
{
throw new NotImplementedException();
}

public string this[int index]
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
}
}

Cheers,
Andy
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
Aug 25 '06 #4

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

Similar topics

7
6522
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
4
7268
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
1
2188
by: Jakob Nielsen | last post by:
I have implemented a container class with IList and bound it to a listbox. Works just fine, but if i then add a new item to my list, the listbox is not updated. Looking at the definition for...
5
18969
by: Muskito | last post by:
Hi, Is it possible to copy the entire List(Of type) object to another List(Of type) object (they are both of the same type ofcourse) - without keeping the reference? There's the CopyTo...
1
1879
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList =...
4
12831
by: Rene | last post by:
According to the documentation, the List<T> type explicitly implements the non generic IList interface. The problem is that no matter how hard I look, I am not able to find this implemetion on...
6
5986
by: nicolas.rolland | last post by:
Would anyone know the reson why IList<Tdoes not implements IList ?? This results in strange behaviours, like typeof(IList).IsAssignableFrom(typeof(List<string>)) --true...
2
2166
by: Tony | last post by:
Hello! According to the documentation we have the following interface IList : ICollection, IEnumerable { I can understand all the methods in this IList } interface ICollection : IEnumerable...
2
4391
by: Tony Johansson | last post by:
Hello! The Array class implements these interfaces IClonable, IList, ICollection and IEnumerable. Note only interfaces are inherited and no classes so these interfaces will be implemented in...
0
7112
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
6974
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
7183
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
7356
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
5448
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
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.