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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:21:04+00:00 2026-05-27T18:21:04+00:00

I have simple base class with single static field. I have numerous classes that

  • 0

I have simple base class with single static field. I have numerous classes that derive from this base class. When I create a derived class, it causes invocation of the base classes static ctor which initializes the static field (Works as expected). The problem is that when I create another derived class, that inherits from same base, the static field in the base is still null, why???? It was initialized by the first class I instantiated.

Should not static fields in base classes have global allocation and be visible (ie. shared) to all derived classes?

My model:

class Base<T>
{

 protected static object s_field = null;

 static Base { s_field = new object(); }
}

class Derived1<T> : Base<T>
{

}

class Derived2<T> : Base<T>
{

}

// ... later in the program

Derived1<int> instance1 = new Derived1<int>(); // initializes static field (s_field in base class) for all derived types

Derived2<double> instance2 = new Derived2<double>(); // the static field is null

(I can see this through the debugger, but should it not already have been initialized by previous line??)

  • 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-27T18:21:05+00:00Added an answer on May 27, 2026 at 6:21 pm

    Since you have changed your code i believe you need to understand how generics works in .NET.

    Static in generics behaves a bit different than in normal cases. For each unique open type T you provide, the base class maintains unique static member value.

    You create another instance of open type double for the same base class via Derived < double > then youll see the concept what i am talking about.

    Here a sample code to demonstrate more clearly :

    public class Base<T>
        {
            public static string str = null;
    
            static Base()
            {
                str = "hello";
    
                Console.WriteLine("Ctor cald");
            }
        }
    
        public class Derived1<T> : Base<T>{}
        public class Derived2<T> : Base<T> { }
    
        public partial class Program
        {
             public static void Main()
            {
                Derived1<int> derv = new Derived1<int>();
                Derived2<double> derv2 = new Derived2<double>();
                Derived2<double> derv3 = new Derived2<double>();
    
    
                Console.ReadKey();
            }      
        }  
    

    Here you shall see only 2 calls for the static Ctor.

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

Sidebar

Related Questions

I have a base class Element and derived two other classes from it, Solid
I have a c# class providing some simple classes and some base class extensions
I have a derived class that's a very thin wrapper around a base class.
I have this in my model: class Person < ActiveRecord::Base RELATIONSHIP_STATUSES = [ single,
Given that I have a class Base that has a single argument constructor with
I have a simple C++ base class, derived class example. // Base.hpp #pragma once
Suppose I have the following (trivially simple) base class: public class Simple { public
I have a very simple scenario, using NHibernate: one abstract base class animal; two
What is good design in this simple case: Let's say I have a base
I have two simple Model classes and a ViewModel... public class GridItem { 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.