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. static constructors

static constructors

Scheduled Pinned Locked Moved C#
question
4 Posts 3 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.
  • Y Offline
    Y Offline
    Yaakov Davis
    wrote on last edited by
    #1

    Hi everybody. There Is a way to change the initialization sequence in static constructors? I have the following code: class MyClass { static int a; static bool b = true; static MyClass() { a = 10; } } The compiler generates the following: class MyClass { static int a; static bool b; static MyClass() { b = true; a = 10; } } I want that member a will be initialized before b does, but I must do it on runtime. In other words, there is some parameter that tells the compiler to place the inline initializations in the end of the static constructor? Thanks a lot, Yaakov

    D S 2 Replies Last reply
    0
    • Y Yaakov Davis

      Hi everybody. There Is a way to change the initialization sequence in static constructors? I have the following code: class MyClass { static int a; static bool b = true; static MyClass() { a = 10; } } The compiler generates the following: class MyClass { static int a; static bool b; static MyClass() { b = true; a = 10; } } I want that member a will be initialized before b does, but I must do it on runtime. In other words, there is some parameter that tells the compiler to place the inline initializations in the end of the static constructor? Thanks a lot, Yaakov

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      No, there isn't. It looks like your going to have to design your code around this little problem and initialize the new instance of the class yourself instead of relying on the compiler to do it for you. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      Y 1 Reply Last reply
      0
      • Y Yaakov Davis

        Hi everybody. There Is a way to change the initialization sequence in static constructors? I have the following code: class MyClass { static int a; static bool b = true; static MyClass() { a = 10; } } The compiler generates the following: class MyClass { static int a; static bool b; static MyClass() { b = true; a = 10; } } I want that member a will be initialized before b does, but I must do it on runtime. In other words, there is some parameter that tells the compiler to place the inline initializations in the end of the static constructor? Thanks a lot, Yaakov

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        Simply set the value of b in the static constructor, after setting a.

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          No, there isn't. It looks like your going to have to design your code around this little problem and initialize the new instance of the class yourself instead of relying on the compiler to do it for you. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          Y Offline
          Y Offline
          Yaakov Davis
          wrote on last edited by
          #4

          Thanks. I have added a suggestion in the Feedback Center. Yaakov

          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