compile time error or runtime error?

This is a discussion on compile time error or runtime error? within the C# Programming forums, part of the General Programming Boards category; Hello everyone, Could anyone let me know why compiler can not make runtime error report in the following code, other ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    compile time error or runtime error?

    Hello everyone,


    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  4. #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 Originally Posted by Magos View Post
    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.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21