another IList question

  • Thread starter Thread starter jjsavage
  • Start date Start date
J

jjsavage

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
 
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<string> IEnumerable<string>.GetEnumerator()
{
// Do implementation here
throw new NotImplementedException();
}

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


HTH,
Andy
 
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<string> IEnumerable<string>.GetEnumerator()
{
// 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
 
Matthias said:
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<string> IEnumerable<string>.GetEnumerator()
{
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
 

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

Back
Top