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

In c# is it possible to pass or return operators like types [closed]
Updating the Identity AspNetUser does'nt work
Cast item to be type of received parametar
Is there a way to make this a selected item when defining the ListItem in C#?
How to work with sockets in UWP(Window 10)
ThreadPool or Task.Factory
Export data from Gridview to Excel in asp.net
How to run C# task method on separate thread without using Task.Run()
Adding App.Config to VS 2015 CodedUI Project breaks Test Execution on Test Agent 2013
Is it possible to specify the response content inside a IHttpActionResult response to be strongly typed? [duplicate]
Asp.Net application - compilation error using IIS
Make Async function run completely in the background
How to directly access objects in array with C# (without using unsafe)? [closed]
(UWP) How to access a viewmodel's properties/methods from within a diff. viewmodel?
Grouping together char constants into a textbox
Sutherland-Hodgman clipping algorithm in C#

Categories

HOME
msbuild
kendo-ui
packet
windows-server-2008
blob
arcgis
jquery-plugins
spotfire
angularjs-directive
aggregate-functions
servicenow
restheart
wkhtmltopdf
aws-sdk-cpp
vichuploaderbundle
distinct
http-status-codes
derby
web-testing
template10
accordion
gpo
udeploy
jq
pixels
windows-mobile
visual-studio-community
adobe-captivate
cloudformation
checkmarx
hierarchy
andengine
intermec
opencsv
ios-autolayout
slim-lang
log4net-configuration
favorites
ragel
respect-validation
anypoint-studio
dotnetnuke-module
const
nscollectionview
keychain
tapply
compiled-query
autodesk-data-management
paperclip
cakephp-3.2
amazon-fire-tv
easyquery
import.io
google-cloud-shell
skype-bots
dotnet-cli
boost-regex
androiddesignsupport
blackberry
jspx
avi
dynamics-nav-2013
android-contentresolver
controlpanel
imageicon
sdkman
serial-communication
self-join
data-representation
information-hiding
novell
diffmerge
livescript
msdropdown
pyalgotrade
sendy
code-smell
new-operator
power-management
starling-framework
google-voice
grunt-contrib-imagemin
mobile-robots
extensibility
source-depot
jython-2.5
kademlia
uikit-dynamics
tasklist
wpml
idtabs
maven-eclipse-plugin
capitalization
openwrap
digg
javah
excellibrary
wtsapi32

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App