建立动态清单C# [英] Build dynamic list c#
本文介绍了建立动态清单C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码可以正确地进行Web服务调用:
This code works correctly to make a web service call:
int numberOfGuests = Convert.ToInt32(search.Guest);
var list = new List<Guest>();
Guest adult = new Guest();
adult.Id = 1;
adult.Title = "Mr";
adult.Firstname = "Test";
adult.Surname = "Test";
list.Add(adult);
Guest adult2 = new Guest();
adult2.Id = 2;
adult2.Title = "Mr";
adult2.Firstname = "Test";
adult2.Surname = "Test";
list.Add(adult2);
Guest[] adults = list.ToArray();
如何使用numberofguests
变量动态创建列表以创建列表?输出必须与显示的输出完全匹配,否则Web服务调用将失败,因此adult.id = 1,adult2.id = 2,adult3.id = 3,依此类推...
How do I build the list dynamically using the numberofguests
variable to create the list? The output has to match the output shown exactly else the web service call fails, so adult.id = 1, adult2.id = 2, adult3.id = 3, etc...
推荐答案
您知道循环吗?
for (int i = 1; i <= numberofGuests; i++) {
var adult = new Guest();
adult.Id = i;
adult.Title = "Mr";
adult.Firstname = "Test";
adult.Surname = "Test";
list.Add(adult)
}
这将从1到numberOfGuests
在循环内运行一次代码,将变量i
设置为当前值.
This runs the code within the loop once from 1 to numberOfGuests
, setting the variable i
to the current value.
这篇关于建立动态清单C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文