gpt4 book ai didi

c# - 从委托(delegate)构造委托(delegate)。新的委托(delegate)指向什么?

转载 作者:行者123 更新时间:2023-11-30 15:38:28 35 4
gpt4 key购买 nike

考虑以下几点:

Action<int, T> a1 = new Action<int, T>(_insert);

Action<int, T> a2 = new Action<int, T>(a1);

a2 指的是什么?它是 a1,a1 的浅拷贝还是 a1 的深拷贝?

最佳答案

a2 正在引用 a1。这是 IL:

.method private hidebysig static void Main() cil managed
{
.entrypoint
.maxstack 3
.locals init (
[0] class [mscorlib]System.Action a1,
[1] class [mscorlib]System.Action a2)
L_0000: nop
L_0001: ldnull
L_0002: ldftn void WebTools.ConsoleTest.Program::Main()
L_0008: newobj instance void [mscorlib]System.Action::.ctor(object, native int)
L_000d: stloc.0
L_000e: ldloc.0
L_000f: ldftn instance void [mscorlib]System.Action::Invoke() #1
L_0015: newobj instance void [mscorlib]System.Action::.ctor(object, native int)
L_001a: stloc.1
L_0020: nop
L_0021: ret
}

在 #1 处,IL 代码引用了 a1 的 Invoke 方法和实例 a1 本身。

浅拷贝意味着 a1 的内容被复制,但没有被复制。对象 a1 被视为黑盒。因此 a2 将使 a1 在 GC 方面保持事件状态。

关于c# - 从委托(delegate)构造委托(delegate)。新的委托(delegate)指向什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11382002/

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