In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what the local variables of the C # iterator are, which can be used for reference by interested friends. I hope you will gain a lot after reading this article.
Let's see if the traversal code is very stable no matter how the specific set changes. And it is also very convenient to extend the new collection class, but adding code will not modify the original code, which is in line with the principle of opening and closing. Of course, Microsoft will not let go of such a good solution. Now that C # 2.0 has built-in support for C # iterators, look at the System.Collections, System.Collections.Generic namespace, all collections implement this interface: IEnumerable, this interface also has a generic version. Notice that there is only one method for this interface: IEnumerator GetEnumerator (); IEnumerator is the interface of the C # iterator, the equivalent of Iterator in my example, which also has a generic version.
So now all collection classes in. Net can be accessed like this:
IEnumerator ienumerator = list.GetEnumerator (); while (ienumerator.MoveNext ()) {object current = ienumerator.Current;}
But this kind of access is too troublesome, so the foreach keyword appears in C#. Let's take a look at what's going on behind foreach.
Public static void Main () {ArrayList list = new ArrayList (); list.Add (1); list.Add (2); list.Add (3); foreach (object item in list) {Console.WriteLine (item.ToString ());}}
Here is its corresponding IL code:
.method private hidebysig static void Main () cil managed {.entrypoint .maxstack 2 .locals init ([0] class [mscorlib] System.Collections.ArrayList list, [1] object item, [2] class [mscorlib] System.Collections.IEnumerator CS$5 $0000 [3] class [mscorlib] System.IDisposable CS$0 $0001) Lang 0000: newobj instance void [mscorlib] System.Collections.ArrayList::.ctor () Lang 0005: stloc.0 Lang 0006: ldloc.0 Lang 0007: ldc.i4.1 Lang 0008: box int32 Lang 000d: callvirt instance int32 [mscorlib] System.Collections.ArrayList::Add (object) Lang 0012: pop Lang 0013: ldloc.0 Lang 0014: ldc.i4.2 Lang 0015: box int32 Lang 001a: callvirt instance int32 [mscorlib] System.Collections.ArrayList::Add (object) Lang 001f: ldloc.0 Lang 0020: ldc.i4.3 Lang 0022: box int32 Lang 0027: callvirt instance int32 [mscorlib] System.Collections.ArrayList::Add (object) Lang 002c: pop Lang 002d: ldloc.0 Lang 002e: callvirt instance class [mscorlib] System.Collections.IEnumerator [mscorlib] System.Collections.ArrayList::GetEnumerator () Lang 0033 : stloc.2 Lhasa 0034: br.s Lhasa 0048 Lhasa 0036: callvirt instance object [mscorlib] System.Collections.IEnumerator::get_Current () Lhasa 003c: stloc.1 Lhasa 003d: ldloc.1 Lhasa 003e: callvirt instance string [mscorlib] System.Object::ToString () Lhasa 0043: call void [mscorlib] System.Console::WriteLine (string) Lang 0048: ldloc.2 Lhasa 0049: callvirt instance bool [mscorlib] System. Collections.IEnumerator::MoveNext () Lang 004e: brtrue.s Lang 0036 Lang 0050: leave.s Lang 0063 Lang 0052: ldloc.2 Lang 0053: isinst [mscorlib] System.IDisposable Lang 0058: stloc.3 Lang 0059: ldloc.3 Lang 005a: brfalse.s Lang 0062 Lang 005c: ldloc.3 Lang 005d: callvirt instance void [mscorlib] System.IDisposable::Dispose () Lang 0062: endfinally Lang 0063: call string [mscorlib] System.Console: : ReadLine () Lhasa 0068: pop Lang 0069: ret .try Log 0034 to Lang 0052 finally handler Lang 0052 to Lang 0063}
You can see from the .locals init that the compiler added two local variables to the C # iterator for us, one of which is the C # iterator.
Lang 002d: ldloc.0 Lang 002e: callvirt instance class [mscorlib] System.Collections.IEnumerator [mscorlib] System.Collections.ArrayList::GetEnumerator () Lang 0033: stloc.2
These three lines of code tell us that we call the GetEnumerator () method of list to get the instance of the C # iterator and assign it to the local variable of the C # iterator that the compiler added for us, and then the instruction Lanc0034: br.s Linea 0048mbr.s is a forced jump. Let's move on.
Ldloc.2 0048: callvirt instance bool [mscorlib] System.Collections.IEnumerator::MoveNext () Thank you for reading this article carefully. I hope the article "what are the local variables of C # iterator" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.