gpt4 book ai didi

c# - 为什么 C# 中的计数器(使用 CRTP)不会对某些类型的对象进行倒计时?

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:45 26 4
gpt4 key购买 nike

<分区>

我已经在 C# 计数器中使用 CRTP 实现了,但我不知道为什么它不会对某些类型进行倒计时:

using System;

using Type1 = C<char>;
using Type2 = C<int>;

class ExistingObjectCounter<CountedType>
{
private static uint existingObjects = 0;

protected ExistingObjectCounter()
{
++existingObjects;
}

~ExistingObjectCounter()
{
Console.WriteLine("Destruction of " + this + " number " + existingObjects);
--existingObjects;
}

public static uint GetNumberOfLivingObjects
{
get
{
return existingObjects;
}
}
}

class C<T> : ExistingObjectCounter<C<T>>
{
public C()
{}
public C(C<T> m)
{}
};

class ZliczaczObiektow
{
public static void createManyCClassWithStringArgument()
{
for(int i=0; i<10;++i)
new C<String>();
}

public static void Main()
{
Type1 c1 = new Type1(), c2 = new Type1(), c3 = new Type1(c2);
var ws1 = new Type2();

Console.WriteLine("existing objects of class " + c1.GetType() + ": "
+ Type1.GetNumberOfLivingObjects);
Console.WriteLine("existing objects of class " + ws1.GetType() + ": "
+ Type2.GetNumberOfLivingObjects);

createManyCClassWithStringArgument();

{
Type1 c4 = new Type1(), c5 = new Type1();
var ws2 = new Type2();

Console.WriteLine("existing objects of class " + c4.GetType() + ": "
+ Type1.GetNumberOfLivingObjects);
Console.WriteLine("existing objects of class " + ws2.GetType() + ": "
+ Type2.GetNumberOfLivingObjects);

createManyCClassWithStringArgument();
}
System.GC.Collect();

Console.WriteLine("existing objects of class " + c1.GetType() + ": "
+ Type1.GetNumberOfLivingObjects);
Console.WriteLine("existing objects of class " + ws1.GetType() + ": "
+ Type2.GetNumberOfLivingObjects);

Console.WriteLine("existing objects of class C<String>: "
+ C<String>.GetNumberOfLivingObjects);

Console.ReadKey();
}
}

输出是:

existing objects of class C`1[System.Char]:  3
existing objects of class C`1[System.Int32]: 1
existing objects of class C`1[System.Char]: 5
existing objects of class C`1[System.Int32]: 2
existing objects of class C`1[System.Char]: 5
existing objects of class C`1[System.Int32]: 2
existing objects of class C<String>: 20
Destruction of C`1[System.String] number 20
Destruction of C`1[System.String] number 19
Destruction of C`1[System.String] number 18
Destruction of C`1[System.String] number 17
Destruction of C`1[System.String] number 16
Destruction of C`1[System.String] number 15
Destruction of C`1[System.String] number 14
Destruction of C`1[System.String] number 13
Destruction of C`1[System.String] number 12
Destruction of C`1[System.String] number 11
Destruction of C`1[System.String] number 10
Destruction of C`1[System.String] number 9
Destruction of C`1[System.String] number 8
Destruction of C`1[System.String] number 7
Destruction of C`1[System.String] number 6
Destruction of C`1[System.String] number 5
Destruction of C`1[System.String] number 4
Destruction of C`1[System.String] number 3
Destruction of C`1[System.String] number 2
Destruction of C`1[System.String] number 1

编辑:我的问题是为什么计数器在某些情况下不会下降,但在另一些情况下会下降?为什么Type1和Type2在程序结束时没有调用析构函数,而另一方面用String参数化的C却调用了析构函数?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com