我有以下的功能,但它是非常长的,肮脏的,我想优化它:的foreach得到下一个项目
//some code
if (use_option1 == true)
{
foreach (ListViewItem item in listView1)
{
//1. get subitems into a List<string>
//
//2. process the result using many lines of code
}
}
else if (use_option2 = true)
{
//1. get a string from another List<string>
//
//2. process the result using many lines of code
}
else
{
//1. get a string from another List<string> (2)
//
//2. process the result using many lines of code
}
这是工作非常好,但它是非常肮脏 我想用这样的:
//some code
if (use_option1 == true)
{
List<string> result = get_item();//get subitems into a List<string>
}
else if (use_option2 = true)
{
//get a string from another List<string>
}
else
{
//get a string from another List<string> (2)
}
//process the result using many lines of code
private void get_item()
{
//foreach bla bla
}
我该如何让get_item函数每次获取列表中的下一个项目?
我读了一些关于GetEnumerator的内容,但我不知道如果这是解决我的问题或如何使用它。
每个选项的处理代码是否相似? – Justin
在第一个代码段中,这个“// 2。使用多行代码处理结果”的评论,这是同一个过程吗? –
是的,它是一样的,但我不能在一个函数内部使用它,因为从开始的代码(/ /某些代码)。所以唯一的选择是从void使用foreach – ShaMora