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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:58:29+00:00 2026-06-13T02:58:29+00:00

Consider this example code: public class A<T> { public static T TheT { get;

  • 0

Consider this example code:

public class A<T> 
{
    public static T TheT { get; set; }
}
public class B : A<string>
{
    static B() {
        TheT = "Test";
    }
}

public class Program {
    public static void Main(String[] args) {
        Console.WriteLine(B.TheT);
    }
}

Here B.TheT is null. However, changing the Main method like this:

public static void Main() {
    new B();
    Console.WriteLine(B.TheT);
}

B.TheT is “Test”, as expected. I can understand that this forces the static constructor to run, but why does this not happen for the first case?

I tried reading the spec, and this caught my attention (§10.12):

[…] The execution of a static constructor is triggered by the first of the
following events to occur within an application domain:

• […]

• Any of the static members of the class type are referenced.

My interpretation of this is that since TheT is not a member of B, the static constructor of B is not forced to be run. Is this correct?

If that is correct, how would I best let B specify how to initialize TheT?

  • 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-06-13T02:58:31+00:00Added an answer on June 13, 2026 at 2:58 am

    A.TheT is “Test”, as expected. I can understand that this forces the static constructor to run, but why does this not happen for the first case?

    Basically, you haven’t really referenced B. If you look in the IL, I think you’ll find that your code was actually equivalent to:

    public static void Main(String[] args) {
        Console.WriteLine(A<string>.TheT);
    }
    

    The compiler worked out that that’s really the member you meant, even though you wrote B.TheT.

    If that is correct, how would I best let A specify how to initialize TheT?

    I would try to avoid doing this to start with, to be honest… but you could always just add a static method to B:

    public static void Initialize() {
        // Type initializer will be executed now.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: static void Main(string[] args) { var persons = new List<Person> {
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
Consider this simple example - public class Person { private String name; private Date
I have a queston regarding double-checked locking. Consider this example: public class Singleton {
Consider the following code: public class EventManager { public Task<string> GetResponseAsync(string request) { CancellationTokenSource
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Let's consider this piece of code as our example: import java.awt.*; class Maze extends
Consider this example var task =Task.Factory.StartNew(()=>Console.WriteLine(test)); task.ContinueWith(antecendent => { ExceptionProcessor.HandleError(task.Exception.Flatten()); }, TaskContinuationOptions.OnlyOnFaulted); In this
Consider the following short code snippet. namespace B { public class Foo { public
Consider the following code. public interface IFoo { } public class Bar { 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.