c# - C#:如何使用循环创建新的结构对象
问题描述
如何通过循环创建新的结构对象。Foreach 通过列表的元素例如?
这是我的结构:
public struct TestStruct
{
public int value1;
public int value2;
public List<string> firststructlist;
public TestStruct(int bab, int bob)
{
value1 = bab;
value2 = bob;
firststructlist = new List<string>();
}
public void Add(string value)
{
if (firststructlist == null)
firststructlist = new List<string>();
firststructlist.Add(value);
}
}
看起来不错,我可以“手动”创建新对象
TestStruct second = new TestStruct(1, 2);
second.value1 = 8;
second.value2 = 9;
second.firststructlist.Add("panapinom");
second.firststructlist.Add("zsakfos");
但在运行时未能通过循环完成。我想象过这样的事情(这当然行不通):
foreach (string something in anything)
{
TestStruct "something" = new TestStruct(1, 2);
// new TestStruct for all "something" in the list
}`enter code here`
请帮忙!
解决方案
推荐阅读
- javascript - 我们如何在反应jsx中添加''+''两个不同的值
- node.js - Gcloud 没有在 nodejs 应用程序中启动 json rpc 客户端
- swift - 在 Swift 中使用领域
- express - Mongoose 密码验证在消息中显示密码
- django - 1048,“列 'last_name' 不能为空” Django AbstractUser
- javascript - 通过 websockets 以二进制形式发送数据将数字编码为文本
- java - 使用 MySQL 和 Java CommandLineRunner 的 Spring 对象保存问题
- visual-studio-code - VS Code保存文件导致的themekit校验和错误
- python - Python:如何构建一个将同一类的多个对象作为输入参数的函数?
- javascript - 使用会话变量进行用户身份验证(Express/NodeJs)