Hi Olaf,
what are you referring to is called unsafe code in C#. To use it, you need
to compile with the /unsafe compile option and mark the method or class in
which you want to use unsafe constructs as 'unsafe'.
To do exactly what you are saying:
unsafe void Test()
{
byte[] test = new byte[100];
test[66] = 20;
fixed (byte* test2 = &test[66])
{
Console.WriteLine("test[66] = {0}, test2[0] = {1}", test[66],
test2[0]);
test2[0] = 100;
Console.WriteLine("test[66] = {0}, test2[0] = {1}", test[66],
test2[0]);
}
}
But I would recommend not to use unsafe code unless you really need it.
First, it doesn't necessarily increase performance, as it prevents the
garbage collector from moving the fixed object. Secondly, your code becomes
unverifiable and thus requires full trust to execute. For non performance
critical scenarios, it could be better to write a wrapper class around the
byte array, for example to access bytes with offset (really just an
example):
public class MyWrapper
{
byte[] data;
int offset;
public MyWrapper(byte[] data, int offset)
{
this.data = data;
this.offset = offset;
}
public byte this[int index]
{
get { return data[index + offset]; }
set { data[index + offset] = value; }
}
}
HTH,
Stefan
"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:41**********************@news.skynet.be...
I have another problem, maybe it is simple to fix.
I have this:
byte[] Test=new byte[100];
But I now want to have a second pointer Test2 to point to a location
inside
this Test.
But with no copying.
Something like this.
byte[] Test2=&Test[66];
The intentions is that Test2[0] == Test[66]
I need this to find a record position inside a memory block that is a
array
of bytes.
And I need this reference to modify the memory directly.
Any idea how to do this?
--
http://www.skyscan.be