views:

472

answers:

5

In C#, if a class has all the correct methods/signatures for an Interface, but doesn't explicitly implement it like:

class foo : IDoo {}

Can the class still be cast as that interface?

+11  A: 

No, it's not like Objective-C and some other languages. You should explicitly declare interface implementation.

Mehrdad Afshari
So it will throw an exception if I cast it as the interface?
JasonW
Yes, if your class doesn't explicity implement the interface you will get an exception if you try to cast it as such.
Justin Niessner
You can only cast an object to an object that appears somewhere in its inheritance chain (this includes interfaces implemented in the inheritance chain).
R. Bemrose
Lest there be any confusion, keep in mind that "explicitly implementing an interface" has another meaning in C#: http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx
Jeromy Irvine
Explicitly *declare* interface implementation is not supposed to mean "explicit interface implementation*
Mehrdad Afshari
@Mehrdad - Indeed, my comment was directed at @Justin's response. Sorry for any confusion.
Jeromy Irvine
It won't really throw an exception because you'll never make it that far. You can't even compile if you have a class which does not implement all the members of the interface.
Rex M
@Rex: The OP clearly specifies that the class has all the required stuff for an interface, but doesn't declare that it implements the interface. In this case, if you try to cast, it will compile but throw InvalidCastException at runtime.
Mehrdad Afshari
+4  A: 

Provided that IDoo has any members, this code won't compile. If IDoo has no members, then yes, the cast is safe (but obviously of limited use).

BnWasteland
good point about the "obvious limited use" +1
Greg B
The OP clearly specifies *if a class has all the correct methods/signatures for an Interface*, but if it doesn't, then your point is notable.
Mehrdad Afshari
+2  A: 
public class A
{
   public void DoSomething();
}

public interface IDoSomething
{
   void DoSomething();
}

public class B : A, IDoSomething
{ }

B satisfies IDoSomething.DoSomething by inheriting from A

ecoffey
+3  A: 

This would basically require Duck Typing to work in C#, which does not happen automatically.

There are some libraries that can do this, though.

Reed Copsey
+15  A: 

Duck Typing

What you are alluding to is referred to as "duck-typing" (named after the idiom "if it looks like a duck, and quacks like a duck, then it must be a duck").

With duck-typing an interface implementation is implicit once you have implemented the relevant members (just as you describe) however .NET does not currently have any broad support for this.

With the emergent dynamic language features planned for the future, I wouldn't be surprised if this were supported natively by the runtime in the near future.

In the mean time, you can synthesise duck-typing via reflection, with a library such as this, which would allow you to do a duck-typed cast like this: IDoo myDoo = DuckTyping.Cast<IDoo>(myFoo)

Some Trivia

Interestingly, there is one small place where duck-typing is in use in C# today — the foreach operator. Krzysztof Cwalina states that in order to be enumerable by the foreach operator, a class must:

Provide a public method GetEnumerator that takes no parameters and returns a type that has two members: a) a method MoveMext that takes no parameters and return a Boolean, and b) a property Current with a getter that returns an Object.

Notice that he makes no mention of IEnumerable nor IEnumerator. Although it is common to implement these interfaces when creating an enumerable class, if you were to drop the interfaces but leave the implementation, your class would still be enumerable by foreach. Voila! Duck-typing! (Example code here.)

Daniel Fortunov
+1 for trivia! Nice one
Jeremy McGee
Thanks. Hadn't time to check duck typing thingy and always forgot about it. You served information on silver plate.
Arnis L.