如何将类的实例添加到变量 C# 中?
for (int i = 0; i < 8; i++)
{
var msg = new Param
{
type = "text",
text = $"{ message[i].VlrParam.Replace("\n", "").Replace("\r", "")}"
};
// What I need to do to acumulate msg variable into a new variable?
}
Run Code Online (Sandbox Code Playgroud)
将对象附加到循环外部存在的列表,而不是仅附加到仅存在于循环内部的变量。例如:
var msgs = new List<Param>();
for (int i = 0; i < 8; i++)
{
msgs.Add(new Param
{
type = "text",
text = $"{ message[i].VlrParam.Replace("\n", "").Replace("\r", "")}"
});
}
// here you have the list of Param objects created in your loop
Run Code Online (Sandbox Code Playgroud)
您可以创建一个参数列表
var listParam = new List<Param>();
for (int i = 0; i < 8; i++)
{
var msg = new Param
{
type = "text",
text = $"{ message[i].VlrParam.Replace("\n", "").Replace("\r", "")}"
};
listParam.Add(msg);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
87 次 |
| 最近记录: |