<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Mutable value types: the good, the bad and the ugly</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx</link><description>Fire up your favorite search engine, type in “mutable value types” and you might just feel a bit of pity for the poor little guys. It seems like everyone hates them. Truth be told, there’s a lot to dislike about them but before we get into the nastiness</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Mutable value types: the good, the bad and the ugly | ASP NET Hosting</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx#9592400</link><pubDate>Thu, 07 May 2009 03:00:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9592400</guid><dc:creator>Mutable value types: the good, the bad and the ugly | ASP NET Hosting</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://asp-net-hosting.simplynetdev.com/mutable-value-types-the-good-the-bad-and-the-ugly/"&gt;http://asp-net-hosting.simplynetdev.com/mutable-value-types-the-good-the-bad-and-the-ugly/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Mutable value types: the good, the bad and the ugly | Microsoft Share Point</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx#9592450</link><pubDate>Thu, 07 May 2009 03:26:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9592450</guid><dc:creator>Mutable value types: the good, the bad and the ugly | Microsoft Share Point</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://microsoft-sharepoint.simplynetdev.com/mutable-value-types-the-good-the-bad-and-the-ugly/"&gt;http://microsoft-sharepoint.simplynetdev.com/mutable-value-types-the-good-the-bad-and-the-ugly/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Mutable value types: the good, the bad and the ugly</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx#9592916</link><pubDate>Thu, 07 May 2009 09:01:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9592916</guid><dc:creator>stic</dc:creator><description>&lt;p&gt;hi,&lt;/p&gt;
&lt;p&gt;maybe the 'SafeSpinLock' is a good candidate for a class that will become a part of .net framework?&lt;/p&gt;
&lt;p&gt;just in case that we will have 100+ different discussions out there how to implement it: initialise it, dispose in controlled manner, pass around, etc..&lt;/p&gt;
</description></item><item><title>re: Mutable value types: the good, the bad and the ugly</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx#9593493</link><pubDate>Thu, 07 May 2009 15:26:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9593493</guid><dc:creator>Nick</dc:creator><description>&lt;p&gt;Why not just use the already present Nullable&amp;lt;T&amp;gt; wrapper?&lt;/p&gt;
&lt;p&gt;SpinLock? spinWrapper;&lt;/p&gt;
&lt;p&gt;done :)&lt;/p&gt;
</description></item><item><title>re: Mutable value types: the good, the bad and the ugly</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx#9593802</link><pubDate>Thu, 07 May 2009 18:11:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9593802</guid><dc:creator>Lucas</dc:creator><description>&lt;p&gt;@Nick, Nullable&amp;lt;T&amp;gt; is still a value type (struct) with copy-by-value semantics, not a reference type, like the sample wrapper class.&lt;/p&gt;
</description></item><item><title>re: Mutable value types: the good, the bad and the ugly</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx#9626501</link><pubDate>Tue, 19 May 2009 03:25:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9626501</guid><dc:creator>commongenius</dc:creator><description>&lt;p&gt;Its interesting that you reference a post from Eric Lippert's blog. Eric has written quite a few blog posts about the importance of choosing value versus reference types based on their semantics, not their implementation. The fact that value types are allocated inline and reference types in their own space on the heap is an implementation detail that should be irrelevant to the design of the type. Performance characteristics change with time and environment, but type semantics are fixed. I am curious why you felt it was necessary to break this rule of design for types like SpinLock and SpinWait.&lt;/p&gt;
</description></item><item><title>re: Mutable value types: the good, the bad and the ugly</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx#9694931</link><pubDate>Thu, 04 Jun 2009 02:39:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9694931</guid><dc:creator>phillips.joshua</dc:creator><description>&lt;p&gt;commongenius,&lt;/p&gt;
&lt;p&gt;Apologies for the slow response. &amp;nbsp;You're concern is quite valid and I assure you we spent a lot of time debating the tradeoffs before we came to a decision. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Ultimately, while we try our best to avoid implementation details there are just some scenarios that we wouldn't have been able to enable if our types required object allocations. When it comes down to it, we felt that our target audience would often throw aside the types we provided and build their own value-type versions that would give them the performance they need. Of course, we introduce new issues, such as the ones described in this post but we think the developers that would use these types are willing to overcome those issues if it meant they didn't need to build their own types from scratch.&lt;/p&gt;
&lt;p&gt;Josh&lt;/p&gt;
</description></item><item><title>re: Mutable value types: the good, the bad and the ugly</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx#9818121</link><pubDate>Sun, 05 Jul 2009 22:13:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9818121</guid><dc:creator>Abhijeet Patel</dc:creator><description>&lt;p&gt;Josh-&lt;/p&gt;
&lt;p&gt;Since value types are typically alive as long as the stack frame on which they are allocated is active(i.e the stack frame the currently executing thread is running on) and are de-allocated(or fall out of scope) when the stack goes away.&lt;/p&gt;
&lt;p&gt;What is the lifetime of a value type(such as SpinLock )when it's wrapped by a reference type(such as SafeSpinLock), are lifetimes of value types the same as the lifetime &amp;nbsp;of the enclosing reference type in this case?&lt;/p&gt;
</description></item><item><title>re: Mutable value types: the good, the bad and the ugly</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx#9818136</link><pubDate>Sun, 05 Jul 2009 22:49:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9818136</guid><dc:creator>Abhijeet Patel</dc:creator><description>&lt;p&gt;Josh-&lt;/p&gt;
&lt;p&gt;&amp;quot;when we retrieve a partition lock via List’s indexer, we actually get a new copy of a SpinLock &amp;quot;&lt;/p&gt;
&lt;p&gt;why would indexing into a List&amp;lt;T&amp;gt; give you a new value type. value types are not boxed when using List&amp;lt;T&amp;gt;&lt;/p&gt;
&lt;p&gt;Also, I ran the following and I noticed that I get the same hashcode for all SpinLocks, can you shed some light on why this is so?&lt;/p&gt;
&lt;p&gt; List&amp;lt;SpinLock&amp;gt; list = new List&amp;lt;SpinLock&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var sl1 = new SpinLock();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var sl2 = new SpinLock();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var sl3 = new SpinLock();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(string.Format(&amp;quot;Item1 Hashcode {0}&amp;quot;,sl1.GetHashCode()));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(string.Format(&amp;quot;Item2 Hashcode {0}&amp;quot;, sl2.GetHashCode()));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(string.Format(&amp;quot;Item3 Hashcode {0}&amp;quot;, sl3.GetHashCode()));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;\n&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;list.Add(sl1);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;list.Add(sl2);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;list.Add(sl3);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ThreadPool.QueueUserWorkItem(_ =&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var str = string.Format(&amp;quot;ThreadID:{0} reading item at index 0,hashcode{1}&amp;quot;, Thread.CurrentThread.ManagedThreadId, list[0].GetHashCode());&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(str);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ThreadPool.QueueUserWorkItem(_ =&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var str = string.Format(&amp;quot;ThreadID:{0} reading item at index 1,hashcode{1}&amp;quot;, Thread.CurrentThread.ManagedThreadId, list[1].GetHashCode());&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(str);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ThreadPool.QueueUserWorkItem(_ =&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var str = string.Format(&amp;quot;ThreadID:{0} reading item at index 2,hashcode{1}&amp;quot;, Thread.CurrentThread.ManagedThreadId, list[2].GetHashCode());&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(str);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//list.ForEach( i=&amp;gt; Console.WriteLine(i.GetHashCode()));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.Read();&lt;/p&gt;
&lt;p&gt;--------&lt;/p&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;p&gt;Item1 Hashcode -1863199827&lt;/p&gt;
&lt;p&gt;Item2 Hashcode -1863199827&lt;/p&gt;
&lt;p&gt;Item3 Hashcode -1863199827&lt;/p&gt;
&lt;p&gt;ThreadID:3 reading item at index 0, &lt;/p&gt;
&lt;p&gt;hashcode -1863199827&lt;/p&gt;
&lt;p&gt;ThreadID:4 reading item at index 1,&lt;/p&gt;
&lt;p&gt;hashcode -1863199827&lt;/p&gt;
&lt;p&gt;ThreadID:3 reading item at index 2,&lt;/p&gt;
&lt;p&gt;hashcode -1863199827&lt;/p&gt;
</description></item><item><title>re: Mutable value types: the good, the bad and the ugly</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx#9821074</link><pubDate>Tue, 07 Jul 2009 03:53:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9821074</guid><dc:creator>phillips.joshua</dc:creator><description>&lt;p&gt;Hi Abhijeet,&lt;/p&gt;
&lt;p&gt;Yes, when you box a value type, they become stored on the heap and, thus, have the same lifetime as the garbage-collected wrapper. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regarding List's indexer: It is because the value types are not boxed that a new copy is given out every time. &amp;nbsp;If the value-types were boxed, they would be wrapped in a reference and that same reference would be passed out of the indexer on each access. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regarding hash codes: &amp;nbsp;See the documentation for Object.GetHashCode. &amp;quot;The default implementation of the GetHashCode method does not guarantee unique return values for different objects.&amp;quot; &amp;nbsp;In fact, for some structs in the BCL, even the overriden GetHashCode function does not return unique values for different objects (try it on Int32). &amp;nbsp;For SpinLock, we chose not to override the default GetHashCode and provide an accurate hashing function simply because we don't want to encourage people to use them like classes and pass them around. &amp;nbsp;Hash functions are typically used to store objects in structures like hash tables, and since that's a bad practice for the reasons listed in this post, we don't want to send mixed signals by actually implementing the method.&lt;/p&gt;
&lt;p&gt;Thanks for the questions! Hope that helps.&lt;/p&gt;
&lt;p&gt;Josh&lt;/p&gt;
</description></item><item><title>re: Mutable value types: the good, the bad and the ugly</title><link>http://blogs.msdn.com/pfxteam/archive/2009/05/07/9592359.aspx#9821699</link><pubDate>Tue, 07 Jul 2009 09:23:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9821699</guid><dc:creator>Abhijeet Patel</dc:creator><description>&lt;p&gt;Thanks for responding.&lt;/p&gt;
&lt;p&gt;I see what you mean,so unless there were a way to pass a reference to the SpinLock contained &amp;quot;inside&amp;quot; the List&amp;lt;SpinLock&amp;gt;, you would always get a copy when you index, and hence the bug.&lt;/p&gt;
&lt;p&gt;Just a thought...if the SpinLock struct implemented an ISpinLock and you had a List&amp;lt;ISpinLock&amp;gt;, you would be able to access the same SpinLock instance via the interface?&lt;/p&gt;
</description></item></channel></rss>