Difference between casting to a class and to an interface

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: yufufi (yufufi_at_ttnet.net.tr)
Date: 07/04/04


Date: Sun, 4 Jul 2004 16:17:48 +0300

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: 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)
  • Re: Interfaces and private types
    ... procedure Init (I: in out Int) is abstract; ... this is illegal because type T does not have the interface Int. ... so that makes it pretty clear that there is a compiler bug. ...
    (comp.lang.ada)
  • Re: sizeof operator implementation
    ... How does C compiler gets value at ... int main{ ... casting 10 to a pointer type; might as well cast 0 and make what you're ...
    (comp.lang.c)
  • Re: Casting the return value of malloc() ?
    ... casting the return value is unwanted. ... including stdlib.h the return value is int (or int * cant remember ... compiler knows different, which it is allowed to but not required to.) ... Since you're assigning the value returned by malloc to a pointer object, ...
    (comp.lang.c)
  • Re: Casting the return value of malloc() ?
    ... casting the return value is unwanted. ... including stdlib.h the return value is int (or int * cant remember ... compiler knows different, which it is allowed to but not required to.) ... Since you're assigning the value returned by malloc to a pointer object, ...
    (comp.lang.c)