I cannot for the life of me understand the logic behind the Range operator of a Span.
I have a the following byte array which contains an IP Protocol header:
---------------------------------------------------------------------------------
| | | | | | | | | | | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 9 |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---------------------------------------------------------------------------------
| other data | src ip | dest ip |
---------------------------------------------------------------------------------
Using the range operator, I need to use the following values to extract the proper 4 byte arrays:
Span<byte> ipHeader = ethernetFrame.IPHeader;
byte[] sourceIpAddress = ipHeader[12..16].ToArray();
byte[] destinationIpAddress = ipHeader[16..20].ToArray();
If instead, I use the Slice method, I end up with:
Span<byte> ipHeader = ethernetFrame.IPHeader;
byte[] sourceIpAddress = ipHeader.Slice(12, 4).ToArray();
byte[] destinationIpAddress = ipHeader.Slice(16, 4).ToArray();
What is the logic behind the range operator that requires me to give an end value that's 1 past the actual ending index value?
I'm also curious from a performance perspective (because this particular code block runs millions of times per minute), would using Slice be more efficient anyway?