lohashows.blogg.se

Quickcast vs normal cast
Quickcast vs normal cast









quickcast vs normal cast

It only converts between compatible types. In C++ the static_cast() is more strict than C like casting. Like one integer pointer can also point character type data, as they are quite similar, only difference is character has 1-byte, integer has 4-bytes. In C like cast sometimes we can cast some type pointer to point some other type data. And using this C++ cast the intensions are conveyed much better. This static_cast() can be spotted anywhere inside a C++ code. This static_cast() gives compile time checking facility, but the C style casting does not support that. The normal cast like (int)x is C style typecasting where static_cast(x) is used in C++. IFoo f1 = customTypeA as IFoo // reference conversion - IL: isinst ConsoleApp1.Here we will see what are the differences between static_cast and normal C style cast. TypeD d1 = customTypeA as TypeD // reference conversion - IL: isinst ConsoleApp1.Program/TypeD Object o1 = intValue as object // box - IL: box System.Int32 Int? wraps = intValue as int? // nullable wrapper - IL: call instance void valuetype System.Nullable`1.ctor(!0) Long dontLose = intValue // implict - IL: conv.i8 Int loseValue = (int)longValue // explicit - IL: conv.i4 IFoo foo = (IFoo)customTypeA // is-a reference - IL: castclass ConsoleApp1.Program/IFoo TypeB typeB = (TypeB)customTypeA // custom explicit casting - IL: call class ConsoleApp1.Program/TypeB ConsoleApp1.Program/TypeB::op_Explicit(class ConsoleApp1.Program/TypeA) Public static explicit operator TypeB(TypeA v) It feels like the you are going to handle the object in a different way. Value Types: Copy boxing and nullable types.Reference Types: IS-A relationship Objects are always the same, just the reference changes.It feels like the object is going to be converted into something else. IS-A relationship: Change reference type, otherwise throws exception.Value Type Explicit: Copy and information might be lost.Value Type Implicit: Copy without losing information.Custom implicit/explicit casting: Usually a new object is created.It seems the two of them are conceptually different. In the special case of converting to a string, every object has a ToString, so your third method may be okay if o isn't null and you think the ToString method might do what you want. On the other hand, if you're trying to cast to a value type (any primitive, or structs such as DateTime), you have to use the straight cast - the as won't work. These tend to be more common and a lot harder to track down once they happens out in the wild, as nearly every line dereferences a variable and may throw one. However, if you don't perform that test, you'll use s later and have a NullReferenceException thrown. With the as operator, if o isn't a string, s is set to null, which is handy if you're unsure and want to test s: string s = o as string The biggest advantage of using the straight cast is that when it fails, you get an InvalidCastException, which tells you pretty much what went wrong. If your comment means that o really really is a string, I'd prefer the straight (string)o cast - it's unlikely to fail. It really depends on whether you know if o is a string and what you want to do with it.











Quickcast vs normal cast