当要排列组合的内容个数不定时,如何写成通用的算法???
public partial class WebForm9 : System.Web.UI.Page
{
List<List<string>> list = new List<List<string>>() {
new List<string>{"A","B","C"},
new List<string>{"D","E","F"},
new List<string>{"G","H","I"},
new List<string>{"J","K","L"}
};
protected void Page_Load(object sender, EventArgs e)
{
int i = 0;
foreach (string s1 in list[0])
{
foreach (string s2 in list[1])
{
foreach (string s3 in list[2])
{
foreach (string s4 in list[3])
{
TextBox1.Text += string.Format("{0} {1} {2} {3} {4}",s1,s2,s3,s4,"\r\n");
i++;
}
}
}
}
TextBox1.Text += i.ToString();
}
}
如何将此例写成动态算法,即:List<List<string>> list的个数不定时,写成通用的算法???
当list的个数为2个时,用2个foreach嵌套,当个数为3个时,用3个foreach嵌套,,当个数为4个时,用4个foreach嵌套,这该如何写成通用的???