Re: Difference between casting to a class and to an interface

From: Mark Broadbent (no-spam-please_at_no-spam-please.com)
Date: 07/04/04


Date: Sun, 4 Jul 2004 15:22:19 +0100

that is a very good shout.
I was under the impression that you could only cast an object to an
interface if said object implementated said interface.
After having a play I see (like you) that this is not true.
It seems that the compiler does not type check a cast to an interface
(whether this is by design or not I do not know?)
However in the runtime the cast is succeeding.
Unfortunately the methods exposed by the interface will not succeed if
accessed because of the incompatible object type -so when you try and run
one through the interface ...BANG! exception.

I can see this functionality of being of some benefit in some instances, but
I would love to know whether this is by design.

Nice post, well done!

-- 
Br,
Mark Broadbent
mcdba , mcse+i
=============
"yufufi" <yufufi@ttnet.net.tr> wrote in message
news:OGtFRlcYEHA.1048@tk2msftngp13.phx.gbl...
> lets say we have a 'shape' class which doesn't implement IComparable
> interface..
>
> compiler doesn't give you error for the lines below..
>
> shape b= new shape();
> IComparable h;
> h=(IComparable)b;
>
> but it complains for the following lines
> shape b= new shape();
> int a;
> a=(int)b;
>
> error is Cannot convert type 'shape' to 'int'
>
> is there difference between casting to a class and casting to an interface
> ?? what does the compiler checks when it is casting to an interface (or
does
> it check anything ?)
>
> Thanx a lot,
>
> yufufi
>
>
>
>
>
>
>
>


Relevant Pages

  • Re: Can generics really produce strongly typed collections?
    ... The compiler won't detect the bug at compile time. ... will try to cast to B, ... The "foreach" statement specifically behaves in the way you ... interface, assuming one exists. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Anders Hejlsberg comment on immutable objects
    ... >>explicit interface implementation exists is so an interface name can ... I wouldn't cast to an ... > variable as a sort of implicit reference, ... readonly const MyStruct myInstance. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Difference between casting to a class and to an interface
    ... does imlpement the interface IComparable. ... will not, as the cast may work at runtime, but doesn't neccessarily. ... the compiler "assumes" that you know what you are doing, and, as whatever o ... type of the declared variable at compile time, and "dynamic type checking", ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: problems typecast,find by function/member name and getting "managed reference"
    ... and then call methods in the interface. ... and add info about it in XML resource list file and designers who via ... *compiler* more information, which clearly isn't relevant at runtime. ... Casting is not relevant if some datais unmodified(in ...
    (microsoft.public.dotnet.languages.csharp)
  • Invalid Cast Error
    ... object that are not part of that interface, ... Trouble is, every time I do this, I get an invalid cast exception. ... object I'm casting to, and the object I'm casting from is the same. ... Whats even stranger is that I can cut the code out, ...
    (microsoft.public.dotnet.framework.aspnet)