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.
  • R Rob Philpott

    Did you cheat?

    Regards, Rob Philpott.

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

    Nope. This is easy, the first time a static method gets invoked, the static constructor is invoked. Yours throws an exception. The second time, the static constructor is not invoked again, so your call succeeds.

    R 1 Reply Last reply
    0
    • L Le centriste

      Nope. This is easy, the first time a static method gets invoked, the static constructor is invoked. Yours throws an exception. The second time, the static constructor is not invoked again, so your call succeeds.

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

      Le Centriste wrote:

      Nope. This is easy

      Why did you get it wrong then? :)

      Regards, Rob Philpott.

      L 1 Reply Last reply
      0
      • R Rob Philpott

        erm... No. But well done for not cheating. :)

        Regards, Rob Philpott.

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

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

        D R 2 Replies Last reply
        0
        • R Rob Philpott

          Le Centriste wrote:

          Nope. This is easy

          Why did you get it wrong then? :)

          Regards, Rob Philpott.

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

          I got it wrong? Then, why did you accuse me of cheating?

          D 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.

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

            You both got the answer wrong :) . He was only joking when he asked if you were cheating...

            L 1 Reply Last reply
            0
            • L Le centriste

              I got it wrong? Then, why did you accuse me of cheating?

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

              Hes playing. But you did get the answer wrong.

              L 1 Reply Last reply
              0
              • D DominLondon

                Hes playing. But you did get the answer wrong.

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

                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
                • D DominLondon

                  You both got the answer wrong :) . He was only joking when he asked if you were cheating...

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

                  He forgot the icon, so I took as a personal attack. ;P

                  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
                                          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