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

  • Home
  • SEARCH
  • 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 7545827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:01:58+00:00 2026-05-30T09:01:58+00:00

I stumbled upon a problem where i need an instance of the class inside

  • 0

I stumbled upon a problem where i need an instance of the class inside its static constructor. I believed it was not possible to do it so i tried the following:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Foo.someString);
        Console.ReadLine();
    }
}

class Foo
{
    public static readonly string someString;

    static Foo()
    {
        someString = new Foo().CreateString();
    }

    private string CreateString()
    {
        return "some text";
    }
}

To my surprise, it works – the output is “some text”. I believed the static constructor must run and complete before instances of the class can be created. This answer shows that this is not necessarily the case. Does this mean that static and instance constructors are independent of each other? And finally, is it safe to do this (create instances in static constructor)?

p.s. Let’s ignore the fact that this can be avoided by using a different approach.

  • 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-30T09:01:59+00:00Added an answer on May 30, 2026 at 9:01 am

    All that the specification says is that the static constructor will be called before any instance of the class is created. But it doesn’t state anything about the fact that this constructor must finish:

    A static constructor is used to initialize any static data, or to
    perform a particular action that needs to be performed once only. It
    is called automatically before the first instance is created or any
    static members are referenced.

    You could perfectly fine create instances of the class inside the static constructor and this is safe.

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

Sidebar

Related Questions

In our project we've stumbled upon the following problem: we need to provide our
We're using Azure CDN, but we've stumbled upon a problem. Before, content could not
I stumbled upon a problem from the last Facebook Hacker Cup (so it's NOT
At my workplace, we just stumbled upon the problem where we need to create
I stumbled upon a problem of how to make work together acts_as_taggable (on steroids)
I have recently stumbled upon a problem with selecting relationship details from a 1
I've stumbled upon a problem. I'm using 'FormBorderStyle.None', and when I click the icon
I was writing some commented PHP classes and I stumbled upon a problem. My
Hy, I got an intersting problem which I stumbled upon. When I double-click a
Aside from scalability issues, has anyone here actually stumbled upon a web-development problem where

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.