基类中的静态字段生命周期

发布于 2024-12-22 04:57:09 字数 717 浏览 3 评论 0原文

我有带有单个静态字段的简单基类。我有许多从这个基类派生的类。当我创建派生类时,它会导致调用基类静态构造函数来初始化静态字段(按预期工作)。问题是,当我创建另一个从相同基类继承的派生类时,基类中的静态字段仍然为空,为什么???它是由我实例化的第一个类初始化的。

基类中的静态字段不应该具有全局分配并且对所有派生类可见(即共享)吗?

我的模型:(

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 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??)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东风软 2024-12-29 04:57:09

既然您已经更改了代码,我相信您需要了解泛型在 .NET 中的工作原理。

泛型中的静态行为与正常情况下略有不同。对于您提供的每个唯一的开放类型 T,基类维护唯一的静态成员值。

您可以通过 Derived double 为同一基类创建另一个开放类型 double 实例。双>然后你就会明白我所说的概念。

这里有一个示例代码可以更清楚地演示:

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();
        }      
    }  

在这里您将只看到静态 Ctor 的 2 次调用。

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.

二智少女 2024-12-29 04:57:09

我认识到自己的错误了!哇,基类实际上是一个模板类:Base。当我像这样创建基础对象时 new Derived(), new Derived(), new Derived()< /code>,这些是完全不同的类型,因此静态字段规则不同,我的理解是静态字段将分配给类型T的族。
我已经更正了上面的示例以反映这一点(在最初的帖子中)。

I realized my mistake! Wow, the base class is actually a template class: Base<T>. When I create object of the base like this new Derived<int>(), new Derived<double>(), new Derived<object>(), these are completely different types and therefore the static field rules are different, my understanding is that the static field will be allocated for family of type T.
I have corrected the example above to reflect this (in the initial post).

不必你懂 2024-12-29 04:57:09

当您将泛型放入图片中时,整个问题就会改变。您对静态成员继承的理解在没有泛型的情况下按预期工作,并且当泛型就位时,该概念仍然有效,但泛型在运行时创建不同的类型。
BaseDerived1 共享相同的静态成员,其中 Derived1是不同的类型运行时的 Base 不与其共享静态成员。

Whole question changes when you put generics into the picture. Your understanding on inheritance of static members works as expected without generics and when Generics are in places, still the concept is valid with the exception that, Generics creates different types at run time.
Base<int> and Derived1<int> share the same static member where as Derived1<decimal> would be a different type than Base<int> at run time which doesn't share the static member with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文