Early in 2001 we've read that .NET's JIT is smart enough to optimize repeated boundary checks.
In the year 2009 we still can verify that this is not the case (no matter how hard you try).
C#:
private int CharAt(int offset) { string text = this.text; return (uint)offset >= (uint)text.Length ? -1 : text[offset]; }
Disassembly:
private int CharAt(int offset) { string text = this.text; 00000000 push ebp 00000001 mov ebp,esp 00000003 mov ecx,dword ptr [ecx+30h] return (uint)offset >= (uint)text.Length ? -1 : text[offset]; 00000006 cmp dword ptr [ecx+8],edx 00000009 jbe 00000017 0000000b cmp edx,dword ptr [ecx+8] 0000000e jae 0000001C 00000010 movzx eax,word ptr [ecx+edx*2+0Ch] 00000015 pop ebp 00000016 ret 00000017 or eax,0FFFFFFFFh 0000001a pop ebp 0000001b ret 0000001c call 74C24C6C 00000021 int 3
P.S. Neither this method is inlined (IL length is 25 bytes).
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u