Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Been a while (years) since I've looked at assembly, and I know close to nothing about MSIL and JIT, so it would be a nice exercise - couldn't resist, so here's just a bit of, possibly redundant, empirical data. Does the <code>IndexOf</code> overload get inlined?</p> <p>Here's a tiny Console app:</p> <pre><code>class Program { static void Main(string[] args) { "hello".Contains("hell"); } } </code></pre> <p>The JIT generates this in an optimized Release build, Any CPU, running in 32 bit. I've shortened the addresses, and removed some irrelevant lines:</p> <pre><code>--- ...\Program.cs "hello".Contains("hell"); [snip] 17 mov ecx,dword ptr ds:[0320219Ch] ; pointer to "hello" 1d mov edx,dword ptr ds:[032021A0h] ; pointer to "hell" 23 cmp dword ptr [ecx],ecx 25 call 680A6A6C ; String.Contains() [snip] </code></pre> <p>The <code>call</code> at 0x00000025 goes here:</p> <p><strong>String.Contains</strong></p> <pre><code>00 push 0 ; startIndex = 0 02 push dword ptr [ecx+4] ; count = this.Length (second DWORD of String) 05 push 4 ; comparisonType = StringComparison.Ordinal 07 call FF9655A4 ; String.IndexOf() 0c test eax,eax 0e setge al ; if (... &gt;= 0) 11 movzx eax,al 14 ret </code></pre> <p>Sure enough, it seems to call, directly, the final <code>String.IndexOf</code> overload with four arguments: three <code>push</code>ed; one in <code>edx</code> (<code>value</code>: "hell"); <code>this</code> ("hello") in <code>ecx</code>. To confirm, this is where the <code>call</code> at 0x00000005 goes:</p> <pre><code>00 push ebp 01 mov ebp,esp 03 push edi 04 push esi 05 push ebx 06 mov esi,ecx ; this ("hello") 08 mov edi,edx ; value ("hell") 0a mov ebx,dword ptr [ebp+10h] 0d test edi,edi ; if (value == null) 0f je 00A374D0 15 test ebx,ebx ; if (startIndex &lt; 0) 17 jl 00A374FB 1d cmp dword ptr [esi+4],ebx ; if (startIndex &gt; this.Length) 20 jl 00A374FB 26 cmp dword ptr [ebp+0Ch],0 ; if (count &lt; 0) 2a jl 00A3753F [snip] </code></pre> <p>... which would be the body of:</p> <pre><code>public int IndexOf(string value, int startIndex, int count, StringComparison comparisonType) { if (value == null) throw new ArgumentNullException("value"); if (startIndex &lt; 0 || startIndex &gt; this.Length) throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); if (count &lt; 0 || startIndex &gt; this.Length - count) throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); ... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload