gpt4 book ai didi

c# - 如何在 C# 的循环内返回结果?

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:18 24 4
gpt4 key购买 nike

本质上,我有一个 Do..While 循环遍历文本文件中的一些行。我想处理一行,返回一个值(有效或无效),然后移至下一行。

我有一个名为 ProcessTXT 的函数,它接受 2 个字符串。新文件的 SourceDestination

有没有办法将 ReturnedValue string = 设置为结果并让后台工作人员检查变量是否已更改?如果是,将此值添加到列表框中?

private void TranslatePOD(string strSource, string strDest,)
{
TextWriter tw = new StreamWriter(strDest);
TextReader tr = new StreamReader(strSource);
do
{
//My Code doing stuff
//Need to send a result somehow now, but i have more work to do in this loop
//Then using tw.writeline() to write my results to my new file
} while (tr.ReadLine() != null);
}

编辑:当前使用 Yield 的测试代码。我的输出是“TestingGround.Form1+d__0”。我做错了什么吗?

namespace TestingGround
{
public partial class Form1 : Form
{
static IEnumerable<string> TestYield(string strSource)
{
TextReader tr = new StreamReader(strSource);
string strCurLine = System.String.Empty;

while ((strCurLine = tr.ReadLine()) != null)
{
yield return strCurLine;
}
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string MySource = System.String.Empty;
MySource = @"C:\PODTest\Export Script\Export\Shipment List.csv";
listBox1.Items.Add(TestYield(MySource));

}
}

最佳答案

Yield通常用于迭代或流式返回结果。网上有很多例子。有 one在 SO 上读取文件。

关于c# - 如何在 C# 的循环内返回结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12012011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com