Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# can I lock a method parameter?

We have a class variable ArrayList binaryScanData in a class. In all methods having access to it, we put lock(binaryScanData) on it because it is shared. Now we want to move one of those methods out to another util class to make it a static method. We will pass that binaryScanData into the method like this:

public static void convertAndSaveRawData(ref MemoryStream output, ref ArrayList binaryScanData)

Our questions are followed:

  1. how can we sychoronize that binaryScanData? can we do the same as it is originally?
  2. ref is necessary? It will only be read in that convertAndSaveRawData method.
like image 512
5YrsLaterDBA Avatar asked Dec 22 '22 03:12

5YrsLaterDBA


1 Answers

The ref isn't necessary, it is only needed if you are going to change the reference itself (ie, assign a new list to it). You can still lock on the object if you like. I recommend locking on SyncRoot, which I'm not sure if ArrayList has. If not, you might consider moving up to List<T>.

like image 176
Matt Greer Avatar answered Jan 02 '23 09:01

Matt Greer