[英]Array to List<t> c#
My current code works and output is correct. 我当前的代码有效,输出正确。 I am pulling data from a data.txt file and have successfully done so to an array using TextFieldParser.
我正在从data.txt文件中提取数据,并已成功使用TextFieldParser将其提取到数组中。 Is there a way to convert my code to a List?
有没有办法将我的代码转换为列表? And how so?
怎么会这样呢? If converting is not an option then any recommendations on where to start with the code?
如果不能进行转换,那么关于从何处开始的任何建议? Basically trying to go from an array to a list collections.
基本上是尝试从数组转到列表集合。
public partial class EmployeeInfoGeneratorForm : Form
{
public EmployeeInfoGeneratorForm()
{
InitializeComponent();
}
// button event handler
private void GenerateButton_Click(object sender, EventArgs e)
{
string[] parts;
if(File.Exists("..\\data.txt"))
{
TextFieldParser parser = new TextFieldParser("..\\data.txt");
parser.Delimiters = new string[] { "," };
while (true)
{
parts = parser.ReadFields();
if (parts == null)
{
break;
}
this.nameheadtxt.Text = parts[0];
this.addressheadtxt.Text = parts[1];
this.ageheadtxt.Text = parts[2];
this.payheadtxt.Text = parts[3];
this.idheadtxt.Text = parts[4];
this.devtypeheadtxt.Text = parts[5];
this.taxheadtxt.Text = parts[6];
this.emp1nametxt.Text = parts[7];
this.emp1addresstxt.Text = parts[8];
this.emp1agetxt.Text = parts[9];
this.emp1paytxt.Text = parts[10];
this.emp1idtxt.Text = parts[11];
this.emp1typetxt.Text = parts[12];
this.emp1taxtxt.Text = parts[13];
this.emp2nametxt.Text = parts[14];
this.emp2addresstxt.Text = parts[15];
this.emp2agetxt.Text = parts[16];
this.emp2paytxt.Text = parts[17];
this.emp2idtxt.Text = parts[18];
this.emp2typetxt.Text = parts[19];
this.emp2taxtxt.Text = parts[20];
this.emp3nametxt.Text = parts[21];
this.emp3addresstxt.Text = parts[22];
this.emp3agetxt.Text = parts[23];
this.emp3paytxt.Text = parts[24];
this.emp3idtxt.Text = parts[25];
this.emp3typetxt.Text = parts[26];
this.emp3taxtxt.Text = parts[27];
}
}
else //Error Message for if File isn't found
{
lblError.Text = "File Not Found";
}
}
}
In your code example there are two arrays. 在您的代码示例中,有两个数组。
parser.Delimiters = new string[] { "," };
Since parser
is a TextFieldParser
, I can see that Delimiters
must be set to a string array. 由于
parser
是TextFieldParser
,我可以看到必须将Delimiters
设置为字符串数组。 So you cannot change it. 所以你不能改变它。
string[] parts;
parts = parser.ReadFields();
This array accepts the result of parser.ReadFields()
. 该数组接受
parser.ReadFields()
的结果。 The output of that function is a string array, so this code can't be changed without breaking the call. 该函数的输出是一个字符串数组,因此在不中断调用的情况下就无法更改此代码。
However, you can immediately convert it to a list afterward: 但是,之后您可以立即将其转换为列表:
var parts = parser.ReadFields().ToList();
There isn't much point to this either. 这也没有多大意义。
An array is just as good as a list when the size of the array/list doesn't change after it is created. 创建数组/列表后大小不变时,数组与列表一样好。 Making it into a list will just add overhead.
放入列表中只会增加开销。
There are a number of problems here. 这里有很多问题。 I'd be inclined to write your code like this:
我倾向于这样编写您的代码:
public static IEnumerable<List<string>> ParseFields(string file)
{
// Use "using" to clean up the parser.
using (var parser = new TextFieldParser(file))
{
parser.Delimiters = new string[] { "," };
// Use end-of-data, not checks for null.
while (!parser.EndOfData)
yield return parser.ReadFields().ToList();
}
}
I'd refactor your code to put the UI updates in one method: 我将重构您的代码以将UI更新放在一种方法中:
private void UpdateText(List<string> parts ) { ... }
You only do something with the last element in the sequence; 您只能对序列中的最后一个元素进行操作; all your previous edits are lost.
您以前所有的编辑都将丢失。 So be explicit about that:
所以要明确一点:
private void GenerateButton_Click(object sender, EventArgs e)
{
// Use a named constant for constant strings used in several places
const string data = "..\\data.txt";
if(!File.Exists(data))
{
lblError.Text = "File Not Found";
} else {
var parts = ParseFields(data).LastOrDefault();
if (parts != null)
UpdateText(parts);
}
}
See how much cleaner that logic looks when you break it up into smaller parts? 看看将逻辑分解成更小的部分时,该逻辑看起来有多干净? It's very pleasant to have methods that fit easily onto a page.
具有可轻松适应页面的方法是非常令人愉快的。
A direct answer to your question: 您问题的直接答案:
Use the List<T>
constructor that takes an IEnumerable<T>
parameter. 使用采用
IEnumerable<T>
参数的List<T>
构造函数。
With that said, I would read Mr. Lippert's answer until you fully understand it. 话虽如此,我会仔细阅读利珀特先生的回答,直到您完全理解为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.