Hi all i searched for IEnumerator but could not get to understand it.it would be great help if anyone helps me.
basically trying to understand the its role in this code
string requestUriString = URL;
if (requestUriString.IndexOf("http://", StringComparison.InvariantCultureIgnoreCase) < 0)
{
requestUriString = "http://" + requestUriString;
}
WebRequest request = WebRequest.Create(requestUriString);
try
{
IEnumerator enumerator;
StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8);
string sSourceString = reader.ReadToEnd();
reader.Close();
string str6 = this.funcParseStringFromString2String(sSourceString, "<div class=\"title\">", "<div class=\"searches_related\">", false);
ArrayList list = new ArrayList();
list = this.funcParseStringFromString2Stringx(str6, "<h3><a ", "</h3>", false);
int num = this.DataGridView2.RowCount + 1;
try
{
enumerator = list.GetEnumerator();
while (enumerator.MoveNe开发者_开发问答xt())
{
string str8 = Conversions.ToString(enumerator.Current);
string str7 = this.funcParseStringFromString2String(str8, "title=\"", "\" href=", false);
string str5 = this.funcParseStringFromString2String(str8, "\" href=\"", "\">", false).Replace("\" class=\"rated", "");
this.DataGridView2.Rows.Add(new string[] { Conversions.ToString(num), Conversions.ToString(false), str7, str5 });
this.lblInsProg.Text = "Done: " + Conversions.ToString(this.DataGridView2.Rows.Count) + " Articles found";
num++;
}
}
finally
{
if (enumerator is IDisposable)
{
(enumerator as IDisposable).Dispose();
}
}
if (list.Count == 0)
{
this.lblInsProg.Text = "No results found for " + this.search.Text + "!";
this.Button11.Enabled = false;
}
else
{
this.Button11.Enabled = true;
}
if (this.DataGridView2.RowCount != 0)
{
this.Button15.Visible = true;
}
return Conversions.ToString(0);
}
catch (WebException exception1)
{
ProjectData.SetProjectError(exception1);
WebException exception = exception1;
ProjectData.ClearProjectError();
}
return Conversions.ToString(0);
This part of the code:
try
{
enumerator = list.GetEnumerator();
while (enumerator.MoveNext())
{
string str8 = Conversions.ToString(enumerator.Current);
//...
}
}
finally
{
if (enumerator is IDisposable)
{
(enumerator as IDisposable).Dispose();
}
}
is basically the same as:
foreach(var value in list)
{
string str8 = Conversions.ToString(value);
//...
}
but the latter is far more readable, don't you think ?
IEnumerator
allows you to iterate over a list, array, etc. (anything that implements IEnumerable
) and process each element one-by-one.
If you're familiar with a foreach
loop in C#, IEnumerator
is what it uses under the covers. For example, this:
List<string> myList = new List<string>() { "one", "two", "three" };
foreach(string elem in myList)
{
Console.WriteLine(elem);
}
Is actually translated to something like this:
List<string> myList = new List<string>() { "one", "two", "three" };
IEnumerator<string> enumerator = list.GetEnumerator();
while(enumerator.MoveNext())
{
string elem = enumerator.Current;
Console.WriteLine(elem);
}
(This is simplified, as there are other cleanup operations that foreach
does, like disposing the enumerator if it implements IDisposable
, but this addresses the relevant portion)
In fact, your entire try-finally
code block is almost a dead-on copy of how the foreach
construct gets translated into IL.
The IEnumerator interface allows the while()
{} part of the code to loop through list
one by one with the MoveNext() method.
It is the C# "iterator"-pattern implementation. Any class that implemented the interface IEnumerator can be iterated over. Often it is used with the foreach syntactic sugar.
It's a somewhat sloppy way of writing it. The try/finally is not really necessary. I would expect that section of the code to look like this:
var list = new ArrayList(); // (or a newer, generic container, like List<T>)
...
foreach (var item in list)
{
string str8 = Conversions.ToString(item);
...
}
It's an Iterator. As far as I know it follow the Iterator pattern
I would read Jon Skeet's book C# in depth, it is the best explanation of this I have seen although the version of the book I am referring to is 3 and the version that is out now is 4 - I don't know if it is covered the same in that version...
This is not as in-depth as the book but this is useful... http://www.yoda.arachsys.com/csharp/csharp2/iterators.html
精彩评论