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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:54:43+00:00 2026-05-13T14:54:43+00:00

Consider the following code: class Program { static Program() { Program.program1.Value = 5; }

  • 0

Consider the following code:

class Program
{
    static Program() {
        Program.program1.Value = 5;
    }

    static List<Program> values = new List<Program>();
    int value;
    int Value
    {
        get { return value; }
        set { 
            this.value = value;
            Program.values.Add(this);
        }
    }

    static Program program1 = new Program { value = 1 };
    static Program program2 = new Program { value = 2 };
    static Program program3 = new Program { value = 3 };

    static void Main(string[] args)
    {
        if (Program.values.Count == 0) Console.WriteLine("Empty");
        foreach (var value in Program.values)
            Console.WriteLine(value.Value);
        Console.ReadKey();
    }
}

It prints only the number 5, and if removed the code in the static constructor, it prints “Empty”.

Is there a way to force static fields to be initialized even whether not used yet?

I need to have a static property named Values with returns all instances of the referred type.

I tried some variations of this code and some works for some types but doesn’t for others.

EDIT: THE SAMPLE ABOVE IS BROKEN, TRY THIS ONE:

class Subclass<T> {
    static Subclass()
    {
        Values = new List<Subclass<T>>();
    }
    public Subclass()
    {
        if (!Values.Any(i => i.Value.Equals(this.Value)))
        {
            Values.Add(this);
        } 
    }

    public T Value { get; set; }

    public static List<Subclass<T>> Values { get; private set; }
}

class Superclass : Subclass<int>
{
    public static Superclass SuperclassA1 = new Superclass { Value = 1 };
    public static Superclass SuperclassA2 = new Superclass { Value = 2 };
    public static Superclass SuperclassA3 = new Superclass { Value = 3 };
    public static Superclass SuperclassA4 = new Superclass { Value = 4 }; 
}

class Program
{
    static void Main(string[] args)
    {
        //Console.WriteLine(Superclass.SuperclassA1); //UNCOMMENT THIS LINE AND IT WORKS
        foreach (var value in Superclass.Values)
        {
            Console.WriteLine(value.Value);
        }
        Console.ReadKey();
    }
}
  • 1 1 Answer
  • 2 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-13T14:54:43+00:00Added an answer on May 13, 2026 at 2:54 pm

    The answer to your question is ‘well, yes’. But one of the two ways of "forcing" it is what you’re already doing.

    The relevant section in the language spec is Static constructors, and specifically:

    The static constructor for a class executes at most once in a given
    application domain. The execution of a static constructor is triggered
    by the first of the following events to occur within an application
    domain:

    • An instance of the class is created.
    • Any of the static members of the class are referenced.

    If a class contains the Main method (Section 3.1) in which execution
    begins, the static constructor for that class executes before the Main
    method is called. If a class contains any static fields with
    initializers, those initializers are executed in textual order
    immediately prior to executing the static constructor.

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

Sidebar

Related Questions

Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);
Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void
Please consider the following code: public class Person ( public string FirstName {get; set;}
Consider the following code: class Program { static void Main(string[] args) { try {
Consider the following code: namespace DisposeTest { using System; class Program { static void
Consider the following code: class Program { public static explicit operator long(Program x) {
Consider the following code: class Program { void Foo<T>() { } static void Main(string[]
Consider the following code: namespace ConsoleApplication1 { class Program { static void Main(string[] args)
Consider the following C# code: class Program { static public void Print(string toPrint) {
Consider following code: public sealed class Program { public static void Main() { System.Console.WriteLine(Hi);

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.