Since TBytes
, TByteDynArray
, and array of Bytes
are all dynamic arrays of bytes, can typed variables be safely typecasted to each other? (If I have a variable of TBytes
can I simply typecast to TByteDynArray
when using a method that defines parameters as TByteDynArray
and vice-versa?)
Such typecasts are perfectly safe in all the Delphi implementations that I have ever encountered.
However, reinterpretation typecasts like this remove type checking, there is always a risk that future changes to the source code can result in hard to trace errors. I would always try to avoid casting if possible. For example, the very simplest thing you can do is to avoid using array of Byte
as a type in your code and switch to TBytes
.
If you must cast then wrap it up in a function to mitigate the risks I describe above.
function Bytes(const B: TByteDynArray): TBytes;
begin
Result := TBytes(B);
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With