gpt4 book ai didi

c# - 如何在struct中初始化char数组

转载 作者:太空狗 更新时间:2023-10-29 22:10:55 26 4
gpt4 key购买 nike

如何在 C# 中初始化结构中的 char 数组我正在这样做

struct cell
{
public char[] domain =new char[16];
public int[] Peers;
public int NumberOfPeers;
public char assignValue;
}

但是它给出了我们不能在结构体中初始化数组的错误!谁能说出正确的方法

最佳答案

你可以使用构造器

struct cell
{
public cell(int charArraySize) : this()
{
domain = new char[arraySize];
}

public char[] domain;
public int[] Peers;
public int NumberOfPeers;
public char assignValue;
}

注意:即使是结构体,您也不应拥有公共(public)字段,而应将它们封装在属性中。

能够改变一个结构的值是一个更糟糕的问题- mutable structs are evil .

关于c# - 如何在struct中初始化char数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13915321/

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