c# – 为什么不允许在实例类型的struct中使用循环引用,但允许使
发布时间:2020-12-15 08:44:09 所属栏目:百科 来源:网络整理
导读:为什么我们可以在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 referenc
为什么我们可以在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 foo,c2不影响foo的大小. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |