Dynamically build an array for web service

后端 未结 5 972
既然无缘
既然无缘 2021-01-27 01:55

I\'m using C# and asp.net to query a web service.

The user will enter the number of guests and then I need to add that number of guests to the web service call. Creati

5条回答
  •  滥情空心
    2021-01-27 02:17

    All the other answers have suggested using List and normally I'd agree - but in this case there's really no need as it seems you know the size beforehand:

    Guest[] guests = new Guest[numberOfGuests];
    for (int i=0; i < numberOfGuests; i++)
    {
        Guest guest = new Guest();
        // Fill in information about the guest here
        // based on the web form
        guests[i] = guest;
    }
    

    That's not to say you shouldn't use a List if that's more convenient in any way - it's just that the (probably) biggest benefit of using a List is that you don't need to know the size in advance. As that's not relevant here (unless I'm missing something) there's not as much reason to use a list.

提交回复
热议问题