Copy byte array in VB.NET Copy byte array in VB.NET sqlite sqlite

Copy byte array in VB.NET


In your code you are only referencing the byte array.
If you want to copy it you need

Dim bt() As Byte if r.fields("mybyteblob").Value Is not Nothing then     dim lenArray = r.fields("mybyteblob").Length    bt = new Byte(lenArray)    Array.Copy(r.fields("mybyteblob").value, bt, lenArray)end if

There is another alternative.
The Buffer class is faster than Array and more appropriate because you are using a byte array

Dim bt() As Byte if r.fields("mybyteblob").Value Is not Nothing then     dim lenArray = r.fields("mybyteblob").Length    bt = new Byte(lenArray)    Buffer.BlockCopy(r.fields("mybyteblob").value, 0, bt, 0, lenArray)end if

Here a good question on the two methods


It is very unusual to run into garbage collection problems if you only write managed code (i.e. no P/Invoke).

Many smart people has put a lot of effort into making garbage collection work without you having to worry about it, so do just that: don't worry about it. Just write your code, and if you run into a specific behavior you don't understand, then ask about that particular behavior (and I can assure you that in 99.9967% [1] of the cases it will not be the garbage collector).

[1] This is not a random number. I've ran into a garbage collection gotcha once in ~10 years of programming. Assuming 10 bugs a day and 300 days of work per year, that makes 29999/30000 bugs which are not garbage-collection related = 99.9967%.