Ok, so it all started with my interest in hash codes. After some reading from a Jon Skeet post, I asked this question . This really interested me in pointer arithmetic that I have almost no experience. So, after reading this page, I started experimenting when I got a rudimentary understanding from there and my other fantastic peers here on SO!
Now I am doing a few more experiments, and I believe that I have exactly duplicated the hash code cycle, which is implemented stringbelow (I reserve the right to make mistakes):
Console.WriteLine("Iterating STRING (2) as INT ({0})", sizeof(int));
Console.WriteLine();
var val = "Hello World!";
unsafe
{
fixed (char* src = val)
{
var ptr = (int*)src;
var len = val.Length;
while (len > 2)
{
Console.WriteLine((char)*ptr);
Console.WriteLine((char)ptr[1]);
ptr += 2;
len -= sizeof(int);
}
if (len > 0)
{
Console.WriteLine((char)*ptr);
}
}
}
But, the results are a little perplexing to me; view. Here are the results:
Iterating STRING (2) as INT (4)
H
l
o
W
r
d
, ptr[1] , ( ) . . , ptr[1] 4 12 ?