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 6027513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:36:15+00:00 2026-05-23T04:36:15+00:00

public class MyClass<T> { public static readonly String MyStringValue; static MyClass() { MyStringValue =

  • 0
public class MyClass<T>
{
    public static readonly String MyStringValue;

    static MyClass()
    {
        MyStringValue = GenerateString();
    }

    private static String GenerateString()
    {
        //Dynamically generated ONCE per type (hence, not const)
    }

    public void Foo()
    {
        Console.WriteLine(MyStringValue);
    }
}

It’s my understanding that the static readonly String won’t get generated until the static constructor is called on the class. But, the static constructor won’t be called until one of the static methods or variables is accessed.

In a multi-threaded environment is it possible to run into issues because of this? Basically, is the static constructor by default singleton locked or do I have to do this myself? That is… do I have to do the following:

private static Object MyLock;

static MyClass()
{
    lock(MyLock)
    {
        if (MyStringValue == null)
            MyStringValue = GenerateString();
    }
}
  • 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-23T04:36:16+00:00Added an answer on May 23, 2026 at 4:36 am

    The static constructor is guaranteed to run only once per instantiated type. So you don’t need your locking.

    Note that it will run once for each generic parameter. And the static fields on the generic class aren’t shared between different generic parameters either.

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

Sidebar

Related Questions

I have the following code : public class MyClass { private readonly string name;
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
This isn't valid code: public class MyClass { private static boolean yesNo = false;
As in: public class MyClass { private static var MyProp = new {item1 =
import javax.swing.*; import java.awt.*; public class MyClass { public static void main(String args[]) {
import java.util.*; public class MyClass { public static void main(String[] args) { List<String> a
Possible Duplicate: Is Java pass by reference? public class myClass{ public static void main(String[]
public static class MyClass { // why it is allowed ? public static Random
How can I get the ConstructorInfo for a static constructor? public class MyClass {
class MyClass { public: void method2() { static int i; ... } }; Will

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.