Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
GeneralRe: compile time error or runtime error? Pin
#realJSOP5-May-08 2:49
mve#realJSOP5-May-08 2:49 
GeneralRe: compile time error or runtime error? Pin
Guffa5-May-08 10:30
Guffa5-May-08 10:30 
GeneralRe: compile time error or runtime error? Pin
George_George4-May-08 18:55
George_George4-May-08 18:55 
GeneralRe: compile time error or runtime error? Pin
Guffa4-May-08 23:28
Guffa4-May-08 23:28 
GeneralRe: compile time error or runtime error? Pin
George_George7-May-08 1:27
George_George7-May-08 1:27 
AnswerRe: compile time error or runtime error? Pin
Daniel Grunwald4-May-08 4:31
Daniel Grunwald4-May-08 4:31 
GeneralRe: compile time error or runtime error? Pin
George_George4-May-08 4:36
George_George4-May-08 4:36 
GeneralRe: compile time error or runtime error? Pin
Daniel Grunwald4-May-08 4:56
Daniel Grunwald4-May-08 4:56 
Part of the confusion here is that "(Type)expression" can have many meanings:
- type conversion
- unboxing
- boxing
- cast

What it means depends on the type of "expression" (the source type) and the type in the parenthesis (the target type).
If there is a conversion operator that converts from source type to target type, then that is used.
So "(long)an_int_variable" works because there is an conversion operator that sign-extends a 32-bit int to a 64-bit long.
Otherwise, if the source type is System.Object (or an interface) and the target type, then unboxing is used. Unboxing "unpacks" the value from box object. The box object must have exactly the correct type for this to work, otherwise you get an InvalidCastException at runtime.
If the source type is a value type and the target type is System.Object (or an interface implemented by that value type), then boxing is used. This means that a new object is created and the value is copied into it.
Otherwise, it's a cast; which basically means that a pointer is re-interpreted to point to a different object type. In this case, no conversion and no object creation happens.

You get a compile time error only if the compiler can see, by looking at source type and target type ONLY, that the conversion/cast would ALWAYS fail.
GeneralRe: compile time error or runtime error? Pin
George_George4-May-08 18:33
George_George4-May-08 18:33 
GeneralRe: compile time error or runtime error? Examples Pin
Daniel Grunwald4-May-08 5:03
Daniel Grunwald4-May-08 5:03 
GeneralRe: compile time error or runtime error? Examples Pin
George_George4-May-08 18:38
George_George4-May-08 18:38 
GeneralRe: compile time error or runtime error? Pin
#realJSOP4-May-08 6:40
mve#realJSOP4-May-08 6:40 
GeneralRe: compile time error or runtime error? Pin
George_George4-May-08 18:41
George_George4-May-08 18:41 
GeneralRe: compile time error or runtime error? Pin
#realJSOP5-May-08 2:47
mve#realJSOP5-May-08 2:47 
GeneralRe: compile time error or runtime error? Pin
Guffa5-May-08 10:33
Guffa5-May-08 10:33 
GeneralRe: compile time error or runtime error? Pin
George_George7-May-08 1:29
George_George7-May-08 1:29 
AnswerRe: compile time error or runtime error? Pin
Charith Jayasundara4-May-08 8:04
Charith Jayasundara4-May-08 8:04 
GeneralRe: compile time error or runtime error? Pin
George_George4-May-08 18:46
George_George4-May-08 18:46 
QuestionNumbers Don't Display According To The Current Culture! Pin
WaleedH3-May-08 23:21
WaleedH3-May-08 23:21 
QuestionDateTime Hour Minute Question Pin
MumbleB3-May-08 22:38
MumbleB3-May-08 22:38 
AnswerRe: DateTime Hour Minute Question Pin
MumbleB3-May-08 22:42
MumbleB3-May-08 22:42 
GeneralRe: DateTime Hour Minute Question Pin
Luc Pattyn3-May-08 23:07
sitebuilderLuc Pattyn3-May-08 23:07 
GeneralRe: DateTime Hour Minute Question Pin
MumbleB3-May-08 23:45
MumbleB3-May-08 23:45 
GeneralRe: DateTime Hour Minute Question [modified] Pin
#realJSOP4-May-08 3:53
mve#realJSOP4-May-08 3:53 
GeneralRe: DateTime Hour Minute Question Pin
PIEBALDconsult4-May-08 5:55
mvePIEBALDconsult4-May-08 5:55 

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.