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

    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