我们正在从 2 台客户端计算机接收两个字符串到我们的第三台服务器计算机。该数组目前是一维的。我们需要将 result
和 answer
数组的每个成员相加并输出第三个数组。但是,我们将它们用作局部变量 string[]
。
我们如何将 answer
和 result
的值添加到单个数组中。
例如:
answer[0]+result[0]= final[0]
..........
answer[76]+result[76]=final[76]
更新了代码
namespace ExampleLib.Server
{
public class Server
{
string[] answer = new string[77];
string[] result = new string[77];
private void ClientReceiveData(object sender, ConnectedClient.NetDataEventArgs e)
{
if (string.IsNullOrEmpty(e.Message) == false)
{
if (e.ID == 0)
{
answer = e.Message.Split(',');
}
if (e.ID==1)
{
result = e.Message.Split(',');
}
var final = answer.Zip(result, (x, y) => x + y).ToArray();
Trace.WriteLine(String.Join(Environment.NewLine, final));
}
}
}
更新 1(使用 Zip 方法):
最佳答案
尝试使用 Zip
方法,如下所示:
var final = answer.Zip(result, (x, y) => x + y).ToArray();
关于c# - 将 2 个本地 string[] 添加到 C# 中的单个数组中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51992282/