Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Just for fun - what's the output of this?

Just for fun - what's the output of this?

Scheduled Pinned Locked Moved C#
30 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D DominLondon

    Hes playing. But you did get the answer wrong.

    L Offline
    L Offline
    Le centriste
    wrote on last edited by
    #13

    Yes, I finally ran it. I am surprised that the static constructor is called twice. Probably because it is throwing an exception.

    D 1 Reply Last reply
    0
    • L Le centriste

      Yes, I finally ran it. I am surprised that the static constructor is called twice. Probably because it is throwing an exception.

      D Offline
      D Offline
      DominLondon
      wrote on last edited by
      #14

      The static constructor is NOT being called twice.

      L 1 Reply Last reply
      0
      • L Le centriste

        So, both me and this guy give the correct answer. You greet him and you accuse me of cheating. You're a plain asshole.

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #15

        That's a bit rude isn't it? Not sure I like you. And neither did I accuse you of cheating. Tut.

        Regards, Rob Philpott.

        L 1 Reply Last reply
        0
        • R Rob Philpott

          ...if your idea of fun is as warped as mine. No Visual Studio allowed.

             static void Main(string\[\] args)
          
              {
          
                  try
          
                  {
          
                      TestStatic.SaySomething("HI");
          
                  }
          
                  catch (Exception error)
          
                  {
          
                      Console.WriteLine(error.InnerException.Message);
          
                  }
          
                  try
          
                  {
          
                      TestStatic.SaySomething("Hi 2");
          
                  }
          
                  catch (Exception error)
          
                  {
          
                      Console.WriteLine(error.InnerException.Message);
          
                  }
          
                  Console.ReadLine();
          
              }
          
          }
          
          
          
          public class TestStatic
          
          {
          
              static TestStatic()
          
              {
          
                  throw new Exception("Afternoon Exception");
          
              }
          
          
          
              public static void SaySomething(string message)
          
              {
          
                  Console.WriteLine(message);
          
              }
          
          }
          

          Regards, Rob Philpott.

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #16

          I didnt think you could compile with a static constructor? I assume i am wrong thou as others have tested? so in which case i would say output is.. HI Hi 2

          Life goes very fast. Tomorrow, today is already yesterday.

          R 1 Reply Last reply
          0
          • D DominLondon

            The static constructor is NOT being called twice.

            L Offline
            L Offline
            Le centriste
            wrote on last edited by
            #17

            yeah, you're right. So, when the static constructor first throws an exception, any attempt to use the static methods (or maybe instance methods) rethrows the exception. I never use static constructors, and such behavior does not make me want to them.

            1 Reply Last reply
            0
            • M musefan

              I didnt think you could compile with a static constructor? I assume i am wrong thou as others have tested? so in which case i would say output is.. HI Hi 2

              Life goes very fast. Tomorrow, today is already yesterday.

              R Offline
              R Offline
              Rob Philpott
              wrote on last edited by
              #18

              Can't compile with a static constructor?! Not sure they'd be much use in the language if that were the case. Sorry, you're wrong too.

              Regards, Rob Philpott.

              M 2 Replies Last reply
              0
              • R Rob Philpott

                ...if your idea of fun is as warped as mine. No Visual Studio allowed.

                   static void Main(string\[\] args)
                
                    {
                
                        try
                
                        {
                
                            TestStatic.SaySomething("HI");
                
                        }
                
                        catch (Exception error)
                
                        {
                
                            Console.WriteLine(error.InnerException.Message);
                
                        }
                
                        try
                
                        {
                
                            TestStatic.SaySomething("Hi 2");
                
                        }
                
                        catch (Exception error)
                
                        {
                
                            Console.WriteLine(error.InnerException.Message);
                
                        }
                
                        Console.ReadLine();
                
                    }
                
                }
                
                
                
                public class TestStatic
                
                {
                
                    static TestStatic()
                
                    {
                
                        throw new Exception("Afternoon Exception");
                
                    }
                
                
                
                    public static void SaySomething(string message)
                
                    {
                
                        Console.WriteLine(message);
                
                    }
                
                }
                

                Regards, Rob Philpott.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #19

                Ok, well, you have a static ctor which will throw an exception the first time the type is used, so it would be:

                Afternoon Exception
                Hi 2

                Correct?

                R 1 Reply Last reply
                0
                • L Lost User

                  Ok, well, you have a static ctor which will throw an exception the first time the type is used, so it would be:

                  Afternoon Exception
                  Hi 2

                  Correct?

                  R Offline
                  R Offline
                  Rob Philpott
                  wrote on last edited by
                  #20

                  Nope!

                  Regards, Rob Philpott.

                  L 1 Reply Last reply
                  0
                  • R Rob Philpott

                    That's a bit rude isn't it? Not sure I like you. And neither did I accuse you of cheating. Tut.

                    Regards, Rob Philpott.

                    L Offline
                    L Offline
                    Le centriste
                    wrote on last edited by
                    #21

                    Hmmm I am not sure of this.... Anyway, my apologies if I offended you.

                    R 1 Reply Last reply
                    0
                    • R Rob Philpott

                      Can't compile with a static constructor?! Not sure they'd be much use in the language if that were the case. Sorry, you're wrong too.

                      Regards, Rob Philpott.

                      M Offline
                      M Offline
                      musefan
                      wrote on last edited by
                      #22

                      hmmm... well as the TestStatic class is not static it can have a constructor, i just didnt know it could be static. but either way any static methods (i.e. SaySomething) can be called without the need to create an instance of your class, which you are not doing in your code. So both calls to the SaySomething method should be successful, can you explain why either would fail? i think not.

                      Life goes very fast. Tomorrow, today is already yesterday.

                      L R 2 Replies Last reply
                      0
                      • R Rob Philpott

                        Nope!

                        Regards, Rob Philpott.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #23

                        Ok, I rethought. The ctor fails, and that is bad. How about

                        Afternoon Exception
                        Afternoon Exception

                        ?

                        R M 2 Replies Last reply
                        0
                        • R Rob Philpott

                          Can't compile with a static constructor?! Not sure they'd be much use in the language if that were the case. Sorry, you're wrong too.

                          Regards, Rob Philpott.

                          M Offline
                          M Offline
                          musefan
                          wrote on last edited by
                          #24

                          what about... Afternoon Exception HI Hi 2 edit: no, that wouldnt happen if an exception is thrown when the program runs then surely it would just crash with the unhandled exception in the static constructor, no?

                          Life goes very fast. Tomorrow, today is already yesterday.

                          1 Reply Last reply
                          0
                          • M musefan

                            hmmm... well as the TestStatic class is not static it can have a constructor, i just didnt know it could be static. but either way any static methods (i.e. SaySomething) can be called without the need to create an instance of your class, which you are not doing in your code. So both calls to the SaySomething method should be successful, can you explain why either would fail? i think not.

                            Life goes very fast. Tomorrow, today is already yesterday.

                            L Offline
                            L Offline
                            Le centriste
                            wrote on last edited by
                            #25

                            Any class can have a static constructor, but it is highly recommended to make sure they never throw exceptions.

                            1 Reply Last reply
                            0
                            • L Lost User

                              Ok, I rethought. The ctor fails, and that is bad. How about

                              Afternoon Exception
                              Afternoon Exception

                              ?

                              R Offline
                              R Offline
                              Rob Philpott
                              wrote on last edited by
                              #26

                              Good man! And we have a winner. Yep, the type initialiser fails both times - in short an exception in a static constructor is very bad news. Well done. :)

                              Regards, Rob Philpott.

                              L 1 Reply Last reply
                              0
                              • M musefan

                                hmmm... well as the TestStatic class is not static it can have a constructor, i just didnt know it could be static. but either way any static methods (i.e. SaySomething) can be called without the need to create an instance of your class, which you are not doing in your code. So both calls to the SaySomething method should be successful, can you explain why either would fail? i think not.

                                Life goes very fast. Tomorrow, today is already yesterday.

                                R Offline
                                R Offline
                                Rob Philpott
                                wrote on last edited by
                                #27

                                musefan wrote:

                                i think not

                                Really, what is wrong with you people? I think so as described below.

                                Regards, Rob Philpott.

                                1 Reply Last reply
                                0
                                • L Le centriste

                                  Hmmm I am not sure of this.... Anyway, my apologies if I offended you.

                                  R Offline
                                  R Offline
                                  Rob Philpott
                                  wrote on last edited by
                                  #28

                                  Not offended at all Sir. :)

                                  Regards, Rob Philpott.

                                  1 Reply Last reply
                                  0
                                  • L Lost User

                                    Ok, I rethought. The ctor fails, and that is bad. How about

                                    Afternoon Exception
                                    Afternoon Exception

                                    ?

                                    M Offline
                                    M Offline
                                    MNantu
                                    wrote on last edited by
                                    #29

                                    Congratz! =) (Shame on me, SHAME ON ME!) ;P

                                    1 Reply Last reply
                                    0
                                    • R Rob Philpott

                                      Good man! And we have a winner. Yep, the type initialiser fails both times - in short an exception in a static constructor is very bad news. Well done. :)

                                      Regards, Rob Philpott.

                                      L Offline
                                      L Offline
                                      Le centriste
                                      wrote on last edited by
                                      #30

                                      I ran your code for the fun of it, and although the exception message is output twice, the type initializer only fails once, as far as the debugger can tell.

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups