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. The Lounge
  3. Static constructors in C# will be the death of me

Static constructors in C# will be the death of me

Scheduled Pinned Locked Moved The Lounge
csharpc++comarchitecture
19 Posts 12 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.
  • C Chris Maunder

    Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:

    cheers, Chris Maunder

    CodeProject.com : C++ MVP

    A Offline
    A Offline
    Andy Brummer
    wrote on last edited by
    #2

    Wait until you throw exceptions from a static constructor. The runtime really doesn't like that, plus you don't get a second chance. For asp.net applications I try to write everything that I would put in a static constructor in a module and manage state during the application start and end events.

    Using the GridView is like trying to explain to someone else how to move a third person's hands in order to tie your shoelaces for you. -Chris Maunder

    1 Reply Last reply
    0
    • C Chris Maunder

      Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:

      cheers, Chris Maunder

      CodeProject.com : C++ MVP

      S Offline
      S Offline
      Shog9 0
      wrote on last edited by
      #3

      Bah, lazy C# coders. Everyone knows, if you want a method called right, call it yourself. C++ coders know better than to put non-trivial code in constructors... :rolleyes:

      ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

      1 Reply Last reply
      0
      • C Chris Maunder

        Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:

        cheers, Chris Maunder

        CodeProject.com : C++ MVP

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #4

        For the same reason that C# doesn't have const, default values, and other nice stuff. Because no thought went into it, beyond what can be done in the designer. And for the same reason that switch statements are so restricted in C#, the designers assume you don't know enough about programming to use a static constructor anyhow. I love some things about C#, but too often, it's just dumb.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        1 Reply Last reply
        0
        • C Chris Maunder

          Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:

          cheers, Chris Maunder

          CodeProject.com : C++ MVP

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #5

          Chris Maunder wrote:

          Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated.

          Because that would mean that the programmers at Microsoft were wrong in the first place. Maybe it's not applicable but have you thought of using a singleton pattern (there's a generic one on this very site which is very handy).  If you look for it it's the one which uses reflection and involves one line of code.


          If you're stuck in a rut: 1) Consult the documentation* 2) Google it 3) Ask a sensible question 4) Try an ancient ritualistic knowledge summoning (:badger::badger::badger:) dance :jig: 5) Phone :bob: * - If the documentation is MSDN > 6.0 then forget it!

          1 Reply Last reply
          0
          • C Chris Maunder

            Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:

            cheers, Chris Maunder

            CodeProject.com : C++ MVP

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #6

            Chris Maunder wrote:

            Instead of having them execute only when an actual object of the class is instantiated.

            :confused: Is that so as a rule, Chris?

            class Program
            {
            static Program()
            {
            Console.WriteLine("static ctor");
            }

            static void Main(string\[\] args)
            {
                Console.WriteLine("Main");
            }
            

            }

            Outputs :-

            static ctor
            Main

            Regards, Nish


            Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
            Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog

            C 2 Replies Last reply
            0
            • N Nish Nishant

              Chris Maunder wrote:

              Instead of having them execute only when an actual object of the class is instantiated.

              :confused: Is that so as a rule, Chris?

              class Program
              {
              static Program()
              {
              Console.WriteLine("static ctor");
              }

              static void Main(string\[\] args)
              {
                  Console.WriteLine("Main");
              }
              

              }

              Outputs :-

              static ctor
              Main

              Regards, Nish


              Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
              Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #7

              Beat me to it, I was about to do a test. b/c it sure seems ludicrous that they would do that. But, sadly, I was not surprised.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              1 Reply Last reply
              0
              • N Nish Nishant

                Chris Maunder wrote:

                Instead of having them execute only when an actual object of the class is instantiated.

                :confused: Is that so as a rule, Chris?

                class Program
                {
                static Program()
                {
                Console.WriteLine("static ctor");
                }

                static void Main(string\[\] args)
                {
                    Console.WriteLine("Main");
                }
                

                }

                Outputs :-

                static ctor
                Main

                Regards, Nish


                Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #8

                I just did a test, the static constructor is called just before a static variable is accessed, or a static method is called. So, just in time, which seems fine to me.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                C 1 Reply Last reply
                0
                • C Chris Maunder

                  Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:

                  cheers, Chris Maunder

                  CodeProject.com : C++ MVP

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #9

                  Are you sure ? I just did some tests, and it seems to always do option (b)

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                  A 1 Reply Last reply
                  0
                  • C Chris Maunder

                    Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:

                    cheers, Chris Maunder

                    CodeProject.com : C++ MVP

                    N Offline
                    N Offline
                    Nemanja Trifunovic
                    wrote on last edited by
                    #10

                    ECMA-334 C# Language Specification: 17.11 Static constructors:[^]


                    Programming Blog utf8-cpp

                    J 1 Reply Last reply
                    0
                    • N Nemanja Trifunovic

                      ECMA-334 C# Language Specification: 17.11 Static constructors:[^]


                      Programming Blog utf8-cpp

                      J Offline
                      J Offline
                      JBurkey
                      wrote on last edited by
                      #11

                      Nemanja Trifunovic wrote:

                      ECMA-334 C# Language Specification: 17.11 Static constructors:[^]

                      Check here for a good explaination[^] J

                      1 Reply Last reply
                      0
                      • C Christian Graus

                        Are you sure ? I just did some tests, and it seems to always do option (b)

                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                        A Offline
                        A Offline
                        Alois Kraus
                        wrote on last edited by
                        #12

                        To get the definitive answer when static ctor (or type initializers) do run here is the blog about it: http://geekswithblogs.net/akraus1/articles/90803.aspx Static ctors are thread safe they are normally called before any static method or field is accessed. This "normally" rule is broken in certain cases to ensure that the initialization sequence does not end up in recursion or a deadlock. Chris I supose you do have a class with statics and circular dependencies if you do not see your static ctor run before first access to your class. This is bad design ;P and should be avoided in favor of a more explicit initialization semantics like the double checked lock pattern. Yours, Alois Kraus

                        1 Reply Last reply
                        0
                        • C Chris Maunder

                          Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:

                          cheers, Chris Maunder

                          CodeProject.com : C++ MVP

                          J Offline
                          J Offline
                          JBurkey
                          wrote on last edited by
                          #13

                          Now, now...Let's not go getting angry. I think there are 2 reasons. First off, they function similarly to ObjectMain in COM, so they were kinda copying that pattern. Second, and more importantly - what order would you like your objects to be called in? You could open a whole rats nest by having various static constructors referencing static members in all your different classes. That's a messy situation that's best left avoided. J

                          1 Reply Last reply
                          0
                          • C Christian Graus

                            I just did a test, the static constructor is called just before a static variable is accessed, or a static method is called. So, just in time, which seems fine to me.

                            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                            C Offline
                            C Offline
                            Chris Maunder
                            wrote on last edited by
                            #14

                            Desktop or ASP.NET? In ASP.NET I'm running through the code again and again and the static constructor isn't being called until an object is being instantiated, even if I call a static method. Regardless: I will hark back to my C++ days and Trust No One. Explicit calls are now the order of the day.

                            cheers, Chris Maunder

                            CodeProject.com : C++ MVP

                            C A 2 Replies Last reply
                            0
                            • C Chris Maunder

                              Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:

                              cheers, Chris Maunder

                              CodeProject.com : C++ MVP

                              C Offline
                              C Offline
                              Christopher Duncan
                              wrote on last edited by
                              #15

                              If you ask me, you're just being unmanageable. :)

                              Author of The Career Programmer and Unite the Tribes www.PracticalStrategyConsulting.com

                              1 Reply Last reply
                              0
                              • C Chris Maunder

                                Desktop or ASP.NET? In ASP.NET I'm running through the code again and again and the static constructor isn't being called until an object is being instantiated, even if I call a static method. Regardless: I will hark back to my C++ days and Trust No One. Explicit calls are now the order of the day.

                                cheers, Chris Maunder

                                CodeProject.com : C++ MVP

                                C Offline
                                C Offline
                                Christian Graus
                                wrote on last edited by
                                #16

                                Chris Maunder wrote:

                                In ASP.NET I'm running through the code again and again and the static constructor isn't being called until an object is being instantiated, even if I call a static method

                                Did you see someone else replied about this ? It works fine for me with a basic test scenario, even in ASP.NET. So, I guess you've run into the specific problem the other poster mentioned.

                                Chris Maunder wrote:

                                Regardless: I will hark back to my C++ days and Trust No One.

                                I trust C++ more than I do C#.

                                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                                1 Reply Last reply
                                0
                                • C Chris Maunder

                                  Desktop or ASP.NET? In ASP.NET I'm running through the code again and again and the static constructor isn't being called until an object is being instantiated, even if I call a static method. Regardless: I will hark back to my C++ days and Trust No One. Explicit calls are now the order of the day.

                                  cheers, Chris Maunder

                                  CodeProject.com : C++ MVP

                                  A Offline
                                  A Offline
                                  Alois Kraus
                                  wrote on last edited by
                                  #17

                                  Hi Chris, sorry to hear you cursing against C#. You are right that there is no way to initialize statics during application startup. But this was never the case even in C++ days. In dlls there is an initialization chain which ensures that during module load the statics are intialized correctly. Things are different with JITed languages since most of the code has not been compiled when the module is loaded. This implies that there is no way to get your static initialized during module load except the CLR guys provide you with a hook to call your C#DllMain. But until then there will be no way initialize all statics during module load at once. From a more distant viewpoint it might even be a good thing that there is no such function anyway. If it would be here I hear you already cursing that you want to do something before C#DllMain was called ;) Your seconds problem that the static ctor aka type initializer has not been called when you are inside a static method should not happen except in grave cirumstances when a race condition occured. Suppose two classes A and B where each of them has a static instance of the other type inside it. This was bad design in C++ and is still in C#. If you have a different scenario where this happens I would be glad to hear about it. Yours, Alois Kraus My Blog

                                  1 Reply Last reply
                                  0
                                  • C Chris Maunder

                                    Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:

                                    cheers, Chris Maunder

                                    CodeProject.com : C++ MVP

                                    C Offline
                                    C Offline
                                    code frog 0
                                    wrote on last edited by
                                    #18

                                    Nah... Old age will kill you but .Net will drive you insane.:^):-D

                                    1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      Why oh why can't we just have static constructors either a) execute at App start, or (if you are picky about not having your app take 5 mins to start up), b) execute before any static method of the class is executed Instead of having them execute only when an actual object of the class is instantiated. :mad:

                                      cheers, Chris Maunder

                                      CodeProject.com : C++ MVP

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

                                      Anyone else mentioned "Do not post programming questions (use the programming forums for that)" ? :rolleyes:

                                      The tigress is here :-D

                                      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