我有以下功能,但它很长,很脏,我想优化它:
//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 的东西,但我不知道这是否是我的问题的解决方案或如何使用它。