gpt4 book ai didi

c# - 为什么不允许在实例类型的结构中循环引用但允许静态类型的循环引用?

转载 作者:太空狗 更新时间:2023-10-29 21:34:02 25 4
gpt4 key购买 nike

为什么在struct中可以有静态循环引用而不能有实例类型循环引用?

struct C
{
//following line is not allowed. Compile time error.
// it's a non static circular reference.
public C c1;
//But this line compiles fine.
//static circular reference.
public static C c2;
}

最佳答案

非静态引用失败,因为您试图使结构成为其自身的一部分,从而导致循环引用。

静态声明有效是因为c2 不是结构本身的一部分;每当您声明例如C fooc2不影响foo的大小。

关于c# - 为什么不允许在实例类型的结构中循环引用但允许静态类型的循环引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20991804/

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