Monday, February 20, 2006

Interface implementation in C#.

I read a chapter about interface implemenation in "Applied Microsoft .Net framework programming" and gain some more understanding on this interface topic.

There are basic two types of interface implementation, one is the implicit implementation, in which case, the public/private/protected modifier have to be specified on the implementation. It is the recommended way to implement the interface. Another type of implementation is explicit implementation, in which case, the modifier cannot be added, but the method has to be preceded with the interface name. This will sometimes hide the implementation details, and IS NOT recommended in most cases.

To call the methods in the "explicit implementation", the object has to be casted to the interface type before calling the actual method.

A good example is the Int32 type in the .Net framework, it implements an interface called IConvertible , but you cannot call the ToDouble(), ToByte().. methods until you have to cast the integer to IConvertible.

No comments: