byte
it doesn’t have much secret is a data type that has 1 byte and the possible values of it go from 0 to 255, it is as if it were a int
, only has a smaller representation capacity of different numbers.
The *
there is no multiplication, this is what is called the pointer, so when you use a byte *
in a variable is saying that the type of this variable is a pointer to byte
and so he is very different from himself byte
, thus creating a indirect and thus access the data itself, the byte, indirectly through this variable.
In this specific case it is used to do pointer arithmetic, so it navigates by a set of bytes one by one. It’s not much different than having one byte[]
(internally this form uses pointer arithmetic, but in a specific way), but it is a much simpler and more efficient way. You can see that increment operator, it’s not adding 1 to the byte value, but to its address, that is, it’s going to the next available address there, as it is a type byte
it goes to the next byte. You can add to the address indefinitely, you can even access some byte improperly, the control is yours, it is not like normal C# code that there is a control, and this is why it is efficient. This case can be seen that it is safe without having to check in each operation if it is within the object area.
This is only used when you need to perform a lot or do something the standard C# way doesn’t allow, and note that this is only possible in a context unsafe
since some memory access security is lost. No wonder it was used in encryption.
This excerpt shows how similar it is to array:
*(byteArray + i)
This is exactly the same as doing:
byteArray[i]
I put in the Github for future reference.
That code is picking up the address contained in byteArray
, adding up the value of i
and then taking the value that is at this address (the so-called indirect), then the pointer (*
) used in this context is how to say "access the value contained in this address). The code of array even has security checks, so it is slower.
Today C# has different ways to get the same result (one of them), with equal efficiency without using code unsafe
and therefore pointers, but not all scenarios the pointer is still expendable.
It is extremely rare to need to use this kind of thing and almost all C# programmers will never use it directly.