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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T12:58:46+00:00 2026-05-28T12:58:46+00:00

I have a static class with a static list property on. I want to

  • 0

I have a static class with a static list property on. I want to generate the content of the list only once, and then be able to use it. I of course also want to ensure that the data is actually created so that when an object uses the class it has data in the list. How do I accomplish this?

public static class MyClass
{
    public static List<int> MyList = new List<int>();

    public MyClass()
    {
        for (int i = 0; i < 10; i++)
        {
            MyList.Add(i);
        }
    }
}
  • 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-28T12:58:46+00:00Added an answer on May 28, 2026 at 12:58 pm
    • I want to generate the content of the list only once
    • I also want to ensure that the data is actually created

    Your current code ensures exactly that, when you change the constructor to a static one:

      static MyClass() { }  // not public or private
    

    The class loader will execute the initialization of MyList before any other member of the class can be executed. This happens the first time the class is used.

    The observable behaviour is that the static members are initalized (once) at the beginning of your application.

    Only when you build something where static initializers depend on each other you may have something to think/worry about.

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

Sidebar

Related Questions

I have a class with static members. I want to get a list of
I have a method I'm writing that uses reflection to list a class's static
I have a static class that I use as my Data Utils for my
I have a static class in a shared project, which I want to extend
i want to use below codes. So i have a generic list<MyClass> i think
I have this code: public static final <TypeVO extends BaseVo> List<SelectItem> populateSelectBoxForType( final Class<TypeVO>
What I have: List Class: Rule --> Class: Term ---> Property Compare_Src Property Compare_Mode
I have defined the following select list: <%= this.Select(Scope) .Options(Scopes.AllValues) // static property to
I have a cart model class that has a List property like so: public
I have a static class with no static constructor, but many static members. I

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.