Generic Interface Implementation in C# Orcas Beta 1

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In C# 2, this works just fine:

public class foo : IEnumerable<string>
{
private string[] _list;
public IEnumerator<string> GetEnumerator()
{
foreach (string s in _list)
yield return s;
}
}

However Orcas complains that:

'myNS.foo' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'. 'myNS.foo.GetEnumerator()'
cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it
does not have the matching return type of 'System.Collections.IEnumerator'.

It appears that I'll have to add an explicit non-generic interface
implementation to appease the compiler:

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

Just wondering whether this is a change in generics implementation or just a
bug in beta 1.

Cheers,
Don
 
Donald Xie said:
In C# 2, this works just fine:

public class foo : IEnumerable<string>
{
private string[] _list;
public IEnumerator<string> GetEnumerator()
{
foreach (string s in _list)
yield return s;
}
}

However Orcas complains that:

'myNS.foo' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'myNS.foo.GetEnumerator()'
cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because
it
does not have the matching return type of
'System.Collections.IEnumerator'.

It appears that I'll have to add an explicit non-generic interface
implementation to appease the compiler:

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

Just wondering whether this is a change in generics implementation or just
a
bug in beta 1.

Cheers,
Don

ive just done a simple binary sorted tree class,
and was confused by this error too,
I ended up inheriting only the non generic collection class stuff
System.Collections.CollectionBase

not sure what the pros and cons are ...

Colin =^.^=
 
Donald,

Nothing has changed, the same error comes up in C# 2.0, so it's not an
error in Orcas. This is what I get in C# 2.0:

Error 1 'foo' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'. 'foo.GetEnumerator()' is
either static, not public, or has the wrong return type.
c:\temp\ConsoleApplication1\ConsoleApplication1\Program.cs 86 14
ConsoleApplication1
 
Hmm, I was using VS2005 SP1, which seems to be happy with this. What version
are you using?

Thanks,
Don

Nicholas Paldino said:
Donald,

Nothing has changed, the same error comes up in C# 2.0, so it's not an
error in Orcas. This is what I get in C# 2.0:

Error 1 'foo' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'. 'foo.GetEnumerator()' is
either static, not public, or has the wrong return type.
c:\temp\ConsoleApplication1\ConsoleApplication1\Program.cs 86 14
ConsoleApplication1

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Donald Xie said:
In C# 2, this works just fine:

public class foo : IEnumerable<string>
{
private string[] _list;
public IEnumerator<string> GetEnumerator()
{
foreach (string s in _list)
yield return s;
}
}

However Orcas complains that:

'myNS.foo' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'myNS.foo.GetEnumerator()'
cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because
it
does not have the matching return type of
'System.Collections.IEnumerator'.

It appears that I'll have to add an explicit non-generic interface
implementation to appease the compiler:

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

Just wondering whether this is a change in generics implementation or just
a
bug in beta 1.

Cheers,
Don
 
Donald Xie said:
Hmm, I was using VS2005 SP1, which seems to be happy with this. What version
are you using?

I'm using VS2005 SP1 and get the same failure.

Complete code:

using System;
using System.Collections.Generic;

public class Test : IEnumerable<string>
{
private string[] _list;
public IEnumerator<string> GetEnumerator()
{
foreach (string s in _list)
yield return s;
}

static void Main()
{
}
}


Compilation (including compiler version number):

C:\Users\Jon\Test>csc Test.cs
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.312
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

Test.cs(4,14): error CS0536: 'Test' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'Test.GetEnumerator()'
is either static, not public, or has the wrong return type.
c:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll: (Location
of symbol
related to previous error)
Test.cs(7,32): (Location of symbol related to previous error)
 
Thanks again Jon. Well I've just tried on my office PC with VS2..5 SP1, and I
get this error too!

I'll double check on my home PC when I get back tonight...

Cheers,
Don

Jon Skeet said:
Donald Xie said:
Hmm, I was using VS2005 SP1, which seems to be happy with this. What version
are you using?

I'm using VS2005 SP1 and get the same failure.

Complete code:

using System;
using System.Collections.Generic;

public class Test : IEnumerable<string>
{
private string[] _list;
public IEnumerator<string> GetEnumerator()
{
foreach (string s in _list)
yield return s;
}

static void Main()
{
}
}


Compilation (including compiler version number):

C:\Users\Jon\Test>csc Test.cs
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.312
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

Test.cs(4,14): error CS0536: 'Test' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'Test.GetEnumerator()'
is either static, not public, or has the wrong return type.
c:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll: (Location
of symbol
related to previous error)
Test.cs(7,32): (Location of symbol related to previous error)
 
Excuse me when I'm pulling my foot from my mouth - the error is on my home PC
as well. Now I'm wondering what I was doing yesterday...

At least this is consistent so I just have to use the workaround.

Thanks for setting me straight Jon ;-)

Cheers,
Don

Donald Xie said:
Thanks again Jon. Well I've just tried on my office PC with VS2..5 SP1, and I
get this error too!

I'll double check on my home PC when I get back tonight...

Cheers,
Don

Jon Skeet said:
Donald Xie said:
Hmm, I was using VS2005 SP1, which seems to be happy with this. What version
are you using?

I'm using VS2005 SP1 and get the same failure.

Complete code:

using System;
using System.Collections.Generic;

public class Test : IEnumerable<string>
{
private string[] _list;
public IEnumerator<string> GetEnumerator()
{
foreach (string s in _list)
yield return s;
}

static void Main()
{
}
}


Compilation (including compiler version number):

C:\Users\Jon\Test>csc Test.cs
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.312
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

Test.cs(4,14): error CS0536: 'Test' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'Test.GetEnumerator()'
is either static, not public, or has the wrong return type.
c:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll: (Location
of symbol
related to previous error)
Test.cs(7,32): (Location of symbol related to previous error)
 
Back
Top