c#


C# List<T> indexer thread safety


Until recently, I had been under the assumption that setting an element of a List<T> via indexer is thread safe in the following context.
// Assumes destination.Count >= source.Count
static void Function<T,U>(List<T> source, Func<T,U> converter, List<U> destination)
{
Parallel.ForEach(Partitioner.Create(0, source.Count), range =>
{
for(int i = range.Item1; i < range.Item2; i++)
{
destination[i] = converter(source[i]);
}
});
}
Since List<T> stores its elements in an array internally and setting one by index shouldn't necessitate resizing, this seemed like a reasonable leap of faith. Looking at the implementation of List<T> in .NET Core however, it appears that the indexer's setter modifies some internal state (see below).
// Sets or Gets the element at the given index.
public T this[int index]
{
get
{
// Following trick can reduce the range check by one
if ((uint)index >= (uint)_size)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
}
Contract.EndContractBlock();
return _items[index];
}
set
{
if ((uint)index >= (uint)_size)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
}
Contract.EndContractBlock();
_items[index] = value;
_version++;
}
}
So should I assume that List<T> is not thread-safe even when each thread is only getting/setting elements from its own portion of the collection?
Have a read here:
https://msdn.microsoft.com/en-us/library/6sh2ey19.aspx#Anchor_10
To answer your question, no - as per the documentation, it's not guaranteed to be thread safe.
Even if the current implementation appeared to be thread safe (which it doesn't, anyway), it would still be a bad idea to make that assumption. Since the documentation explicitly says it's not thread safe - future versions may legally change the underlying implementation to no longer be thread safe and break any assumption you previously relied on.

Related Links

Accessing httpcontext in class library with ASP.NET Core 1.1 (MVC)
C# - Unable to add item to listview from method invoked from class
FormFlow : Cancel form after some times
Why do C# methods have 2 separate flags: HasThis and IsStatic
C# - System.FormatException: Invalid format string at System.DateTime
Unity, removing a keycode that is already selected
How do I use a .NET core class library in a .NET core application?
Database is not updating after running stored procedure in asp app
Reference a dll inside a folder within the bin folder
WPF: Open Calendar like DatePicker in popup
ASP.NET MVC Custom Optimization - How could I create a timestamp?
int? version available for String.IsNullOrWhiteSpace [duplicate]
How to implement “user interactive” dependency injection in ASP MVC
Server Error in '/' Application. The resource cannot be found. View cannot see controller?
select query for read data in multiple sheet excel using oledb
treat AbsoluteUri C#

Categories

HOME
twitter-bootstrap
leaflet
intellij-idea
snappy
dronekit
reference
conceptual
xss
localhost
confluence-rest-api
mediarecorder
browser-cache
country-codes
wkhtmltopdf
google-app-maker
osclass
adroitlogic
expo
yui
jinja2
android-actionbar
google-calendar-api3
md2
aws-devicefarm
passbook
android-wifi
advertising
bootstrap-accordion
ps
windowserror
chartist.js
oracle10g
scalatest
mongoose-im
restangular
intermec
storefront
microstrategy
ansible-inventory
ng-lightning
java-5
result
angular-fullstack
automapper-4
spring-cloud-bus
intermediate-language
androiddesignsupport
rmq
tsc
zsh-completion
distcc
foreground
scriptengine
change-password
vitamio
shtml
overlapping
angular-promise
recode
database-project
transport
windows-phone-8-emulator
vensim
clearquest
photogrammetry
data-representation
pymol
esri-arc-engine
caption
powerpoint-2010
scjp
pyalgotrade
sendy
record-locking
wpa
source-depot
kademlia
uikit-dynamics
linqdatasource
junit-rule
custom-cursor
wxperl
mscorlib
peoplepicker
lightopenid
subdirectories
database-agnostic
dgml
focus-stealing
file-storage
projectgen
ffdshow

Resources

Encrypt Message