Home > Archive > C# > August 2004 > Casting Question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Garrett 2004-07-23, 3:57 pm |
| Hi all,
I have a question about casting. Here is a line of code from
Microsoft's Data Access Block:
using (SqlConnection clonedConnection =
(SqlConnection)((ICloneable)connection).Clone())
My question is this: why is it necessary to cast the connection object
to the ICloneable interface before calling the Clone method? If it
implements IClonable anyway, why the cast? Shouldn't the Clone method
have an implementation in the object?
| |
| hsaleem 2004-08-03, 4:05 pm |
| it is called Explicit interface implementation
example
interface IClone
{
object Clone();
}
public class HardDisk : IClone
{
public HardDisk()
{
}
}
HardDisk obj = new HardDisk();
object cloned = ((IClone)obj).Clone();
reference "C# Language Specification.doc"
13.4.1 Explicit interface member implementations
Explicit interface member implementations serve two primary purposes:
• Because explicit interface member implementations are not accessible
through class or struct instances, they allow interface
implementations to be excluded from the public interface of a class or
struct. This is particularly useful when a class or struct implements
an internal interface that is of no interest to a consumer of that
class or struct.
• Explicit interface member implementations allow disambiguation of
interface members with the same signature. Without explicit interface
member implementations it would be impossible for a class or struct to
have different implementations of interface members with the same
signature and return type, as would it be impossible for a class or
struct to have any implementation at all of interface members with the
same signature but with different return types.
Thanks,
hsaleem
agarrettb@hotmail.com (Garrett) wrote in message news:<3a5b722f.0407230635.3202c250@posting.google.com>...
> Hi all,
>
> I have a question about casting. Here is a line of code from
> Microsoft's Data Access Block:
>
> using (SqlConnection clonedConnection =
> (SqlConnection)((ICloneable)connection).Clone())
>
> My question is this: why is it necessary to cast the connection object
> to the ICloneable interface before calling the Clone method? If it
> implements IClonable anyway, why the cast? Shouldn't the Clone method
> have an implementation in the object?
|
|
|
|
|