多重价值 [英] Multiple value
本文介绍了多重价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当它循环3次并返回所有值时,如何在单个变量中保留多个值?
例如:
杂货店(鸡蛋,苹果,坚果);
杂货店(葡萄酒,大米,面包);
杂货店(面包,鸡蛋,面粉);
退货;->返回所有3个值.
Hi,
How can I keep multiple values in a single variable when it loop for 3 times and return all value?
ex:
grocery(egg,apple,nut);
grocery(wine,rice,bread);
grocery(bread,egg,flour);
return grocery;--> return all 3 value.
推荐答案
使用以下代码创建通用字符串列表并在其中添加项目:
Use the following code for creating a generic string list and add item in it:
System.Collections.Generic.List<string> itemList = new System.Collections.Generic.List<string>();
itemList.Add("egg");
itemList.Add("apple");
itemList.Add("nut");
现在在迭代列表时,请使用
之类的foreach语句
Now at the time of iterating the list use foreach statement like
foreach (string item in itemList)
{
Console.WriteLine(item);
}
很简单...不是吗? :)
Simple...isn''t it? :)
编写一个从Collection类派生的类.重写ToString()以循环并返回类中的所有项目.像下面的例子:
Write a class derived from Collection class. Override ToString() to loop and return all the items in the class. Like the example below:
using System;
using System.Collections.ObjectModel;
using System.Text;
public class Groceries : Collection<string>
{
public Groceries()
{
}
public override string ToString()
{
StringBuilder result = new StringBuilder();
foreach (string item in this.Items)
{
if (result.Length > 0)
{
result.Append(", ");
}
result.Append(item);
}
return result.ToString();
}
}
返回数组,返回列表,返回结构...问题出在哪里?
Return array, return list, return struct... Where is the problem?
这篇关于多重价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文