No, you need to use a lock block to ensure that no one is trying to read the array while some other process is writing to it. Otherwise, you are likely to have problems.
This is actually quite simple in C#, you can just do this:
// declare an object to use for locking
Object lockObj = new Object();
// declare the array
int[] x = new int[5];
// set the array value (thread safe operation)
public void SetArrayVal(int ndx, int val)
{
if (ndx < 0 || ndx >= x.Length)
{
throw new ArgumentException("ndx was out of range", ndx);
}
lock (lockObj )
{
x[ndx] = val;
}
}
// get the array value (thread safe operation)
public int GetVal(int ndx)
{
if (ndx < 0 || ndx >= x.Length)
{
throw new ArgumentException("ndx was out of range", ndx);
}
lock (lockObj )
{
return x[ndx];
}
}
I wouldn't worry so much about the performance here as to making sure you get the thread saftiness correct, which is critical.