简体   繁体   English

C#foreach超出范围

[英]C# foreach out of scope

For some reason, my array summary , subject and date are getting use of unassigned local variable errors. 由于某种原因,我的数组summarysubjectdate正在use of unassigned local variable错误。 Does anybody know whats going on? 有人知道发生了什么吗?

    int count = 0;
    string[] summary;
    string[] subject;
    string[] date;

    foreach (SyndicationItem item in feed.Items)
    {
        subject[count] = item.Title.Text;
        date[count] = item.PublishDate.DateTime.ToString();

        summary[count] = item.Summary.Text;
        list.InnerHtml += "<a href=\"#\" onclick\"showArticle(" + count + ");\"><li id=\"post" + count + "\"><b>" + subject[count] + "</b><br>" +
           "<p id=\"posted-date\">Posted on: " + date[count] + "</p>" + "</li></a>";
        count++; 
    } 

You need to initialize the arrays to a particular size, eg: 您需要将数组初始化为特定大小,例如:

string[] summary = new string[feed.Items.Count()];

Alternatively, you can use a List<String> instead of an array if you don't know or care about the length of the arrays. 另外,如果您不知道或不关心数组的长度,则可以使用List<String>代替数组。 A list is actually preferable in your situation, as the Count() extension method will unnecessarily enumerate feed.Items prior to your loop. 实际上,最好使用列表,因为Count()扩展方法会在循环之前不必要地枚举feed.Items

You never assign a value to the array, you only ever access the array through its indexer even though you never created it. 您永远不会为数组分配值,即使您从未创建过数组,也只能通过其索引器访问数组。 You could initialize them like so: 您可以像这样初始化它们:

string[] summary = new string[feed.Items.Count];
string[] subject = new string[feed.Items.Count];
string[] date = new string[feed.Items.Count];

They are not initialized . 它们没有初始化 You should complete the declaration like: 您应该像这样完成声明:

string[] summary = new string[feed.Items.Count];
string[] subject = new string[feed.Items.Count];
string[] date = new string[feed.Items.Count];

You need to initialize the arrays before you can use them, like this: 您需要先初始化数组,然后才能使用它们,如下所示:

string[] summary = new string[length];

However, in this case you don't know the length beforehand, because feed.Items is an IEnumerable<SyndicationItem> , so you can't get the number of items directly (you could use .Count() , but it would make you enumerate the collection several times, which is usually not recommended). 但是,在这种情况下,您事先不知道长度,因为feed.ItemsIEnumerable<SyndicationItem> ,因此您无法直接获取项目数(可以使用.Count() ,但是这样做会使您多次枚举集合,通常不建议这样做)。

A better approach would be to use lists instead of arrays: 更好的方法是使用列表而不是数组:

List<string> summary = new List<string>();

...

summary.Add(item.Summary.Text)

...

Previous answers have correctly identified that you need to initialize the array variables with the size of the array. 先前的答案已正确确定您需要使用数组的大小初始化数组变量。 To think about why this is necessary realize that at the start of the foreach loop the program has no idea how much memory these arrays are going to take up. 考虑为什么这样做是必要的,要意识到在foreach循环的开始,程序不知道这些数组将占用多少内存。 It would be impossible to say where in memory summary[count] is going to point to! 不可能说出summary[count]指向的位置!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM