c# - What is the difference between locking the specified object and locking a secondary object? -


I recently asked a friend who is just starting to play around with the thread, which What is the difference between using a secondary object purely for locking or locking the object that you are referring to, I have to admit that I did not know, can anyone tell me? I'll try and display with some code snippets:

The first method:

  list & lt; String & gt; Data = new list & lt; String & gt; (); Object datalock = new object (); Void main () {lock (datalock) {if (data.contains ("SomeSearchString")) {// something with the data and then delete it}}}  

Another way: < / P>

  list & lt; String & gt; Data = new list & lt; String & gt; (); Void main {} (if data.contains ("SomeSearchString")) {// do something with the data and then delete it}}}  

What's important Is it the difference or down for this personal coding style? If there is any significant difference, can anyone explain what it is?

I came to another question [] in which the answer was that these two are equal, but if this is the case for best use and why?

I have seen many instances scattered around the net, I use the first method as a matter of personal choice, but I wonder if the ability to use will be another way.

The big difference is that with the secondary object only your code About the address Other code (unintentional results, pulse / wait, etc.) locking against the primary object (which is usually available for other codes).

IMO, Probably the CLR team would have done better with the typical lock type - or the monitor should be non-static, etc.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -