Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7554149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:15:41+00:00 2026-05-30T11:15:41+00:00

namespace MyNameSpace { static class MyClass { static MyClass() { //Authentication process.. User needs

  • 0
namespace MyNameSpace
{
    static class MyClass
    {
        static MyClass()
        {
            //Authentication process.. User needs to enter password
        }

        public static void MyMethod()
        {
            //Depends on successful completion of constructor
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyClass.MyMethod();
        }
    }
}

Here is the sequence which I assumed

  1. Start of static constructor
  2. End of static constructor
  3. Start of main
  4. Start of MyMethod
  5. End of main

Now in any scenario if 4 will start before 2 I am screwed. Is it possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:15:42+00:00Added an answer on May 30, 2026 at 11:15 am

    You only asked one question here but there are a dozen or so questions that you should have asked, so I’ll answer them all.

    Here is the sequence which I assumed

    1. Start of class constructor (also known as cctor)
    2. End of cctor
    3. start of Main
    4. start of MyMethod

    Is this correct?

    No. The correct sequence is:

    1. Start of cctor for Program, if there is one. There is not.
    2. End of cctor for Program, if there is one. There is not.
    3. Start of Main
    4. Start of cctor for MyClass
    5. End of cctor for MyClass
    6. Start of MyClass.MyMethod

    What if there is a static field initializer?

    The CLR is permitted to change the order in which static field initializers run in some cases. See Jon’s page on the subject for details:

    The differences between static constructors and type initializers

    Is it ever possible for a static method like MyMethod to be called before the cctor of that class completes?

    Yes. If the cctor itself calls MyMethod then obviously MyMethod will be called before the cctor completes.

    The cctor does not call MyMethod. Is it ever possible for a static method like MyMethod to be called before the cctor of MyClass completes?

    Yes. If the cctor uses another type whose cctor calls MyMethod then MyMethod will be called before the MyClass cctor completes.

    No cctors call MyMethod, directly or indirectly! Now is it ever possible for a static method like MyMethod to be called before the cctor of MyClass completes?

    No.

    Is that still true even if there are multiple threads involved?

    Yes. The cctor will finish on one thread before the static method can be called on any thread.

    Can the cctor be called more than once? Suppose two threads both cause the cctor to be run.

    The cctor is guaranteed to be called at most once, no matter how many threads are involved. If two threads call MyMethod “at the same time” then they race. One of them loses the race and blocks until the MyClass cctor completes on the winning thread.

    The losing thread blocks until the cctor is done? Really?

    Really.

    So what if the cctor on the winning thread calls code that blocks on a lock previously taken by the losing thread?

    Then you have a classic lock order inversion condition. Your program deadlocks. Forever.

    That seems dangerous. How can I avoid the deadlock?

    If it hurts when you do that then stop doing that. Never do something that can block in a cctor.

    Is it a good idea to rely upon cctor initialization semantics to enforce complex security requirements? And is it a good idea to have a cctor that does user interactions?

    Neither are good ideas. My advice is that you should find a different way to ensure that the security-impacting preconditions of your methods are met.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

namespace MyNamespace { static void foo1() { } } using namespace MyNamespace; class MyClass
// MyClass.h namespace MyNamespace { static const double GasConstant = 1.987; class MyClass {
I have something like this: namespace MyNamespace { public partial class MyClass: UserControl {
Lets say I have the following class: namespace myNamespace { [TypeLibType((short)2)] [ClassInterface((short)0)] [ComImport] public
I have MyCustomWidget in a namespace MyNameSpace namespace MyNameSpace{ class MyCustomWidget : public QWidget{
When I create a JS namespace (myNamespace) with a public method (myPublicMethod) jsfile1.js var
I can forward declare a function in a namespace by doing this: void myNamespace::doThing();
using namespace std; class A { public: A() {} ~A() {} map<int, string*>& getMap()
I have a class like this: public static class MyFeedExtensions { private readonly static
namespace MyNamespace { public struct MyStruct { public string MyString; public int MyInt; public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.