Log in
Register
What's new
Search
Search
Search titles only
By:
Menu
Log in
Register
What's new
Search
Search
Search titles only
By:
Forums
Tutorials
About
Rules
What's New
Driver Reference Table
Donate
Search titles only
By:
Latest activity
Register
Hardware, Networking & Other Support
Programming
LINQ to MSIL Test
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="AceInfinity" data-source="post: 22220" data-attributes="member: 4"><p>So I wanted to see how some LINQ looked in MSIL. I took this quick and simple query which returns an <strong>IEnumerable<Int32></strong>:</p><p></p><p>[code]from char c in "012345678".ToCharArray()</p><p>select Int32.Parse(c.ToString())[/code]</p><p></p><p>And I got this:</p><p>[code]IL_0001: ldstr "012345678"</p><p>IL_0006: callvirt System.String.ToCharArray</p><p>IL_000B: call System.Linq.Enumerable.Cast</p><p>IL_0010: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1</p><p>IL_0015: brtrue.s IL_002A</p><p>IL_0017: ldnull </p><p>IL_0018: ldftn b__0</p><p>IL_001E: newobj System.Func..ctor</p><p>IL_0023: stsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1</p><p>IL_0028: br.s IL_002A</p><p>IL_002A: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1</p><p>IL_002F: call System.Linq.Enumerable.Select</p><p></p><p>b__0:</p><p>IL_0000: ldarga.s 00 </p><p>IL_0002: call System.Char.ToString</p><p>IL_0007: call System.Int32.Parse</p><p>IL_000C: stloc.0 </p><p>IL_000D: br.s IL_000F</p><p>IL_000F: ldloc.0 </p><p>IL_0010: ret [/code]</p><p></p><p>What's happening in the above, is the load string (ldstr) opcode is being called for our initial string value to get loaded onto the stack, so that we can make calls to the later methods we want to use. (Everything should be loaded onto the stack before our methods are to perform any kind of instructions on them.) Next, a late bound call is made to the System.String.ToCharArray() function on our loaded string popped from the stack,</p><p></p><p>Then we load our function which takes the char.ToString and calls the Int32.Parse method on it, since the parse method is expected to succeed, we pop the result off the top of the stack, and place it into our new variable at index 0, and proceed to load the modified local variable at index 0 onto the stack before calling to return.</p><p></p><p>When we do this, as you can see we embed a System.Func<> delegate, where a char is converted to a 32 bit signed integer output.</p><p></p><p>After all that is done, we call upon System.Linq.Enumerable.Select which casts the elements of the sequence on the stack to an IEnumerable object.</p></blockquote><p></p>
[QUOTE="AceInfinity, post: 22220, member: 4"] So I wanted to see how some LINQ looked in MSIL. I took this quick and simple query which returns an [b]IEnumerable<Int32>[/b]: [code]from char c in "012345678".ToCharArray() select Int32.Parse(c.ToString())[/code] And I got this: [code]IL_0001: ldstr "012345678" IL_0006: callvirt System.String.ToCharArray IL_000B: call System.Linq.Enumerable.Cast IL_0010: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1 IL_0015: brtrue.s IL_002A IL_0017: ldnull IL_0018: ldftn b__0 IL_001E: newobj System.Func..ctor IL_0023: stsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1 IL_0028: br.s IL_002A IL_002A: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1 IL_002F: call System.Linq.Enumerable.Select b__0: IL_0000: ldarga.s 00 IL_0002: call System.Char.ToString IL_0007: call System.Int32.Parse IL_000C: stloc.0 IL_000D: br.s IL_000F IL_000F: ldloc.0 IL_0010: ret [/code] What's happening in the above, is the load string (ldstr) opcode is being called for our initial string value to get loaded onto the stack, so that we can make calls to the later methods we want to use. (Everything should be loaded onto the stack before our methods are to perform any kind of instructions on them.) Next, a late bound call is made to the System.String.ToCharArray() function on our loaded string popped from the stack, Then we load our function which takes the char.ToString and calls the Int32.Parse method on it, since the parse method is expected to succeed, we pop the result off the top of the stack, and place it into our new variable at index 0, and proceed to load the modified local variable at index 0 onto the stack before calling to return. When we do this, as you can see we embed a System.Func<> delegate, where a char is converted to a 32 bit signed integer output. After all that is done, we call upon System.Linq.Enumerable.Select which casts the elements of the sequence on the stack to an IEnumerable object. [/QUOTE]
Insert quotes...
Verification
Post reply
Hardware, Networking & Other Support
Programming
LINQ to MSIL Test
Menu
Log in
Register
Top