Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# multithreading

Can ICollection<T>.Count property getter be considered atomic (and, therefore, threadsafe)?

Thank you.

like image 620
Yippie-Ki-Yay Avatar asked Aug 29 '26 13:08

Yippie-Ki-Yay


2 Answers

It completely depends on the collection; ICollection<T> is just an interface.

Every collection that I know of simply returns an Int32 field in its Count getter, so it usually is atomic.
However, there is never a guarantee that the count didn't change just after you fetched the property.

like image 91
SLaks Avatar answered Sep 01 '26 07:09

SLaks


No, as you can only make that assumption for implementations, not an interface.

like image 25
Femaref Avatar answered Sep 01 '26 07:09

Femaref



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!