![]() |
| | #1 |
| Registered User Join Date: May 2006
Posts: 1,579
| compile time error or runtime error? Could anyone let me know why compiler can not make runtime error report in the following code, other than throw cast exception during runtime? Code: static void Main()
{
int i = 100;
object b = i;
long l = (long)b; // why compiler can not make type check here?
return;
}
thanks in advance, George |
| George2 is offline |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Did you mean to say: "why compiler can not make compile time error report in the following code"?
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline |
| | #3 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,125
| Because b is an "object" which can be a "long" (which is valid cast) but also something else (which might be invalid cast). The compiler does not know, and far from all cases are as easy to detect as the one you show.
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is offline |
| | #4 | |
| Registered User Join Date: May 2006
Posts: 1,579
| Thanks laserlight and Magos, Are there any documents about why type cast/conversion are checked by C# compiler? :-) Quote:
regards, George | |
| George2 is offline |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Beginner Needs help in Dev-C++ | Korrupt Lawz | C++ Programming | 18 | 05-02-2009 12:27 PM |
| Another syntax error | caldeira | C Programming | 31 | 09-05-2008 01:01 AM |
| Post... | maxorator | C++ Programming | 12 | 10-11-2005 08:39 AM |
| ras.h errors | Trent_Easton | Windows Programming | 8 | 07-15-2005 10:52 PM |
| Linking error | DockyD | C++ Programming | 10 | 01-20-2003 05:27 AM |