Click here to Skip to main content
15,911,646 members
Home / Discussions / C#
   

C#

 
GeneralRe: Downcasting VS Interfaces - Performance perceptive Pin
Roger Alsing21-Feb-08 23:44
Roger Alsing21-Feb-08 23:44 
GeneralRe: Downcasting VS Interfaces - Performance perceptive Pin
N a v a n e e t h22-Feb-08 3:20
N a v a n e e t h22-Feb-08 3:20 
GeneralRe: Downcasting VS Interfaces - Performance perceptive Pin
Pete O'Hanlon21-Feb-08 23:57
mvePete O'Hanlon21-Feb-08 23:57 
GeneralRe: Downcasting VS Interfaces - Performance perceptive Pin
N a v a n e e t h22-Feb-08 3:18
N a v a n e e t h22-Feb-08 3:18 
GeneralRe: Downcasting VS Interfaces - Performance perceptive Pin
Pete O'Hanlon22-Feb-08 4:28
mvePete O'Hanlon22-Feb-08 4:28 
GeneralRe: Downcasting VS Interfaces - Performance perceptive Pin
Guffa22-Feb-08 0:28
Guffa22-Feb-08 0:28 
GeneralRe: Downcasting VS Interfaces - Performance perceptive Pin
N a v a n e e t h22-Feb-08 3:17
N a v a n e e t h22-Feb-08 3:17 
GeneralRe: Downcasting VS Interfaces - Performance perceptive Pin
Mike Dimmick22-Feb-08 1:37
Mike Dimmick22-Feb-08 1:37 
The compiler knows at compile time that Car is derived from Vehicle, and the runtime allows a reference to a derived class to be stored in a reference to its base class. Compiling with optimizations on and debugging off (a release build) gives the following code:
.method public hidebysig static class Vehicle CreateVehicle() cil managed
{
    .maxstack 8
    L_0000: newobj instance void Car::.ctor()
    L_0005: ret 
}
Note no cast instructions at all. All it's done is create a new Car and return it.

Going the other way:
// C# code
public static Car ConvertToCar( Vehicle v )
{
    return (Car) v;
}
 
// IL code
 
.method public hidebysig static class Car ConvertToCar(class Vehicle v) cil managed
{
    .maxstack 8
    L_0000: ldarg.0 
    L_0001: castclass Car
    L_0006: ret 
}
This time the code can't know at compile time that the parameter v passed into ConvertToCar is actually a Car object, not a Bike or anything else deriving from Vehicle, so it generates a castclass instruction. This instruction checks that the actual type is either a Car or something deriving from Car, if so it modifies the reference as necessary (actually nothing needs to be done, the vtable pointer is always fine for base to derived conversions). If not it throws an InvalidCastException.

If you wanted to get a null reference instead you should use the as instruction - this is better if you're not certain that it's the right class. Adding two more methods:
// C# code
public static Car TryConvertToCar( Vehicle v )
{
    return v as Car;
}
 
public static bool IsCar( Vehicle v )
{
    return v is Car;
}
 
// IL code
.method public hidebysig static class Car TryConvertToCar(class Vehicle v) cil managed
{
    .maxstack 8
    L_0000: ldarg.0 
    L_0001: isinst Car
    L_0006: ret 
}
 
.method public hidebysig static bool IsCar(class Vehicle v) cil managed
{
    .maxstack 8
    L_0000: ldarg.0      // push v onto stack
    L_0001: isinst Car   // convert item on stack to a Car,
                         // or replace with null if it isn't one
    L_0006: ldnull       // push a null onto the stack
    L_0007: cgt.un       // Is the second item on the stack greater
                         // (unsigned) than the first? Replace top item
                         // with result
    L_0009: ret          // return top item on stack as result
}
Note that using 'is' gives the effective equivalent code to:
public static bool IsCar( Vehicle v )
{
    Car c = v as Car;
    return c != null;
}
Therefore I prefer using 'as' in all cases, because in almost all circumstances I want to manipulate the object as a Car rather than just find out if it is.

As a design question, I'd use a type field rather than inheritance unless I had a reason to use the polymorphism available through inheritance - one or more virtual functions with different behaviour in different classes in the hierarchy. It's simpler to understand and generally produces less indirection and less metadata.

(Code compiled by dropping into a .cs file and running csc /t:library /o+ /debug- vehicles.cs, and decompiled into IL with Reflector[^].)


DoEvents: Generating unexpected recursion since 1991

GeneralRe: Downcasting VS Interfaces - Performance perceptive Pin
N a v a n e e t h22-Feb-08 3:16
N a v a n e e t h22-Feb-08 3:16 
GeneralDatagridview, checkbox and text in same cell Pin
akkram21-Feb-08 22:27
akkram21-Feb-08 22:27 
GeneralRe: Datagridview, checkbox and text in same cell Pin
Ravenet22-Feb-08 3:17
Ravenet22-Feb-08 3:17 
GeneralApp prevents windows to shutdown Pin
GermanDM21-Feb-08 22:00
GermanDM21-Feb-08 22:00 
GeneralRe: App prevents windows to shutdown Pin
Xmen Real 22-Feb-08 1:16
professional Xmen Real 22-Feb-08 1:16 
GeneralRe: App prevents windows to shutdown Pin
GermanDM22-Feb-08 1:22
GermanDM22-Feb-08 1:22 
GeneralRe: App prevents windows to shutdown Pin
Xmen Real 22-Feb-08 1:27
professional Xmen Real 22-Feb-08 1:27 
GeneralRe: App prevents windows to shutdown Pin
GermanDM22-Feb-08 1:31
GermanDM22-Feb-08 1:31 
GeneralPropertyGrid Pin
stancrm21-Feb-08 21:12
stancrm21-Feb-08 21:12 
GeneralRe: PropertyGrid Pin
phannon8621-Feb-08 21:52
professionalphannon8621-Feb-08 21:52 
GeneralRe: PropertyGrid Pin
stancrm21-Feb-08 22:05
stancrm21-Feb-08 22:05 
GeneralRe: PropertyGrid Pin
phannon8621-Feb-08 22:19
professionalphannon8621-Feb-08 22:19 
GeneralRe: PropertyGrid Pin
Pete O'Hanlon21-Feb-08 22:51
mvePete O'Hanlon21-Feb-08 22:51 
GeneralRe: PropertyGrid Pin
visualhint27-Feb-08 15:05
visualhint27-Feb-08 15:05 
GeneralListView item Pin
Maddie from Dartford21-Feb-08 20:57
Maddie from Dartford21-Feb-08 20:57 
GeneralRe: ListView item Pin
phannon8621-Feb-08 22:10
professionalphannon8621-Feb-08 22:10 
QuestionHow to save a text in a image format Pin
D i x y21-Feb-08 20:56
D i x y21-Feb-08 20:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.