例如,假设我想制作一个遗传学模拟器,并且我有一个非常简单的结构
public struct person{
string name;
int age;
string hairColor;
person father;
person mother;
}
这样我以后就可以用 Joey.father.haircolor 来引用 Joey 父母的头发颜色?我不断收到错误:
Struct Member 'person.father' of type 'person' causes a cycle in the struct layout
使用课程是我唯一的选择吗?为了速度起见,我更喜欢使用结构体,因为它都是数据,但如果没有其他选择,我显然可以只使用类。
出于多种原因,您的类型 Person 应该是一个类:
Struct 不能自引用,因为这会创建无限递归定义。您唯一的选择是使用课程。
这只能通过 C# 中的不安全(非托管)代码实现。
在 C(不是 C#)结构中当然可以引用相同的类型:
#include <stdio.h>
struct my_struct {
int id;
struct my_struct *parent;
};
int main() {
struct my_struct parent_struct = {
.id = 0
};
struct my_struct *parent_pointer = &parent_struct;
struct my_struct child_struct = {
.id = 1, .parent = parent_pointer
};
printf("Child's parent id is %d", child_struct.parent->id);
}
但是在 C# 中,托管代码是不可能的:
public struct MyStruct
{
public MyStruct(string name)
{
Name = name;
}
public string Name { get; set; }
// does not compile
public MyStruct Parent { get; set; }
}
然而,在 C# 中使用非托管代码是可能的:
internal class Program
{
private static void Main(string[] args)
{
unsafe
{
var parent = new MyStruct("parent");
var child = new MyStruct("child");
child.Parent = &parent;
var parentName = child.Parent->Name;
Console.WriteLine(parentName);
}
}
}
public struct MyStruct
{
public MyStruct(string name)
{
Name = name;
}
public string Name { get; set; }
public unsafe MyStruct* Parent { get; set; }
}