Your first example uses an explicit interface implementation:
Explicit Interface Implementation - C# Programming Guide | Microsoft Docs[
^]
This is normally reserved for cases where the class implements two interfaces, and both interfaces contain a member with a specific name, but the member type or signature is not compatible.
The obvious example here is
IEnumerable<T>
, which implements
IEnumerable
. Both interfaces have a member called
GetEnumerator
, but the return type is different, so at least one version has to use an explicit interface implementation.
public class MySequence : IEnumerable<Foo>
{
public IEnumerator<Foo> GetEnumerator() { ... }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
}
This can also be used if you want to have a different implementation of the same member from different interfaces. But this is usually a "code smell", and indicates that there's something wrong with your design.
NB: Neither of your examples represent the "best idea to implement (an) interface", since neither of them conform to the C# naming guidelines. Interfaces should start with the letter
I
, and method names should be
PascalCase
, not
camelCase
.