Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abstract explicit interface implementation in C#

I have this C# code:

abstract class MyList : IEnumerable<T> {     public abstract IEnumerator<T> GetEnumerator();      //abstract IEnumerator IEnumerable.GetEnumerator(); } 

As is, I get:

'Type' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'.

remove the comment and I get:

The modifier 'abstract' is not valid for this item

How do I make an explicit implementation abstract

like image 311
BCS Avatar asked Apr 27 '09 20:04

BCS


People also ask

What is explicit implementation of interface?

An explicit interface implementation is a class member that is only called through the specified interface. Name the class member by prefixing it with the name of the interface and a period. For example: C# Copy.

What is implicit and explicit implementation of interface?

Resolving conflicts here ..... "Implicit Implementation" - means just simple implementation of a particular method having same name and same signature which belongs to the same class itself, where as "Explicit Implementation" - means implementation of a method using their Interface name having same name and signature ...

What is the use of explicit interface implementation in C#?

An Interface is a collection of loosely bound items that have a common functionality or attributes. Interfaces contain method signatures, properties, events etc. Interfaces are used so that one class or struct can implement multiple behaviors.

What is abstract and interface in C?

Abstract class can contain methods, fields, constants, etc. Interface can only contains methods, properties, indexers, events. It can be fully, partially or not implemented. It should be fully implemented.


1 Answers

Interesting - I'm not sure you can. However, if this is your real code, do you ever want to implement the non-generic GetEnumerator() in any way other than by calling the generic one?

I'd do this:

abstract class MyList<T> : IEnumerable<T> {     public abstract IEnumerator<T> GetEnumerator();      IEnumerator IEnumerable.GetEnumerator()      {         return GetEnumerator();     } } 

That saves you from the tedium of having to implement it in every derived class - which would no doubt all use the same implementation.

like image 199
Jon Skeet Avatar answered Sep 21 '22 18:09

Jon Skeet