<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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>C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx</link><description>(I&amp;#39;m giving up with the numbering now, unless anyone particularly wants me to keep it up. What was originally going to be a limited series appears to be growing without end...) As Chris Nahr pointed out in my previous post , my earlier idea about</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Readonly locals? | keyongtech</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1663407</link><pubDate>Sun, 18 Jan 2009 16:46:53 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1663407</guid><dc:creator>Readonly locals? | keyongtech</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Readonly locals? | keyongtech&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1663407" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1646725</link><pubDate>Thu, 04 Sep 2008 10:35:06 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646725</guid><dc:creator>VekiPeki</dc:creator><description>&lt;p&gt;@Jon: &amp;quot;I&amp;#39;m pretty ignorant about XML serialization&amp;quot; -- I find this hard to believe :)&lt;/p&gt;
&lt;p&gt;But anyway I found a way to do it at the end, so I will just post a quick answer if anyone comes across the same problem:&lt;/p&gt;
&lt;p&gt;I ended up using Reflection to change the state of the object inside the implementation of IXmlSerializable.ReadXml, while leaving the private fields readonly.&lt;/p&gt;
&lt;p&gt;I made a new ImmutableObject class as a base class for other immutable classes which I would like to make xml-serializable, so all the messing with Reflection is confined to this base class only.&lt;/p&gt;
&lt;p&gt;Thanks anyway - you actually answered it, I came across one of your posts: &lt;a rel="nofollow" target="_new" href="http://bytes.com/forum/post2178883-17.html"&gt;bytes.com/.../post2178883-17.html&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Veki&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646725" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1646444</link><pubDate>Tue, 02 Sep 2008 06:09:47 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646444</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Veki: I&amp;#39;m afraid I don&amp;#39;t know. I don&amp;#39;t even know if it&amp;#39;s possible. I&amp;#39;m pretty ignorant about XML serialization, I&amp;#39;m afraid :(&lt;/p&gt;
&lt;p&gt;Jon&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646444" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1646438</link><pubDate>Tue, 02 Sep 2008 05:44:40 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646438</guid><dc:creator>VekiPeki</dc:creator><description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve posted this same question to a couple of other places but was unable to find the answer:&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve been looking around for a simple example of the &amp;quot;right way&amp;quot; to implement immutable object xml serialization/deserialization.&lt;/p&gt;
&lt;p&gt;Since IXmlSerializable.ReadXml(System.Xml.XmlReader reader) requires the object inner state to be changed, I have to make private fields writable, which I would like to avoid. I&amp;#39;ve tried googling it but was unable to find the answer I was looking for.&lt;/p&gt;
&lt;p&gt;Thanks a lot,&lt;/p&gt;
&lt;p&gt;Veki &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646438" width="1" height="1"&gt;</description></item><item><title>Lessons learned from Protocol Buffers, part 1: messages, builders and immutability</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1645224</link><pubDate>Wed, 20 Aug 2008 16:31:56 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1645224</guid><dc:creator>Jon Skeet: Coding Blog</dc:creator><description>&lt;p&gt;My port of the Protocol Buffers project has proved pretty interesting. I thought I&amp;#39;d share some of&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1645224" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1625209</link><pubDate>Tue, 20 May 2008 21:12:37 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1625209</guid><dc:creator>Keith J. Farmer</dc:creator><description>&lt;p&gt;Obviously, this has had a lot of thought on many fronts. &amp;nbsp;I&amp;#39;d like to see readonly properties in general, not just autoprops. &amp;nbsp;This is a little more complex, since it potentially means that readonly fields *could* be referred to outside the constructor, but only in other readonly blocks.&lt;/p&gt;
&lt;p&gt;With these, and getter/setter args instead of (or in addition to) property-scoped fields, I&amp;#39;d be happy to do away with (non-property-scoped) fields altogether.&lt;/p&gt;
&lt;p&gt;public readonly int IntValue&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; get;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; set(field) &lt;/p&gt;
&lt;p&gt; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (field &amp;lt; 0) throw new ArgumentOutOfRangeException();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;field = value; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;} &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1625209" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1616158</link><pubDate>Wed, 07 May 2008 11:26:31 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1616158</guid><dc:creator>Andrew Shapira</dc:creator><description>&lt;p&gt;Here&amp;#39;s some more discussion of immutable objects in C#: &lt;a rel="nofollow" target="_new" href="http://onezero.org/?p=blog-immutability1"&gt;onezero.org&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1616158" width="1" height="1"&gt;</description></item><item><title>Pages tagged "immutable"</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1563567</link><pubDate>Mon, 31 Mar 2008 11:15:16 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1563567</guid><dc:creator>Pages tagged "immutable"</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Pages tagged &amp;quot;immutable&amp;quot;&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1563567" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1547370</link><pubDate>Wed, 19 Mar 2008 10:13:11 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1547370</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Oh it's definitely worth being cautious of *any* new feature in a language - and I can certainly see how some of Java's features haven't been as carefully thought out as they might have been.&lt;/p&gt;
&lt;p&gt;The good thing about just blogging on these matters is that I know my blog is read by much smarter people than myself (or at the very least people with much more experience of language design than I have). I can make straw man proposals which may have *some* good points along with distinct downsides. I'm relying on the professionals to polish the good stuff while avoiding the problems :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1547370" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1547364</link><pubDate>Wed, 19 Mar 2008 10:03:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1547364</guid><dc:creator>Ben</dc:creator><description>&lt;p&gt;Sorry, ignore the type system aspect. I had a very specific design in mind to cover the general case and I shouldn&amp;#39;t have brought it up so vaguely on a blog.&lt;/p&gt;
&lt;p&gt;I agree that what you&amp;#39;re asking is the ideal, but my point was you don&amp;#39;t sacrafice much by working with what you have smartly. And from the Java world, everyone I meet seems to have their own language extension proposal, so perhaps I&amp;#39;m a bit overly cautious!&lt;/p&gt;
&lt;p&gt;Extensive deep cloning is horrible, but very careful usages is fine. In both cases you&amp;#39;ll get validation and a runtime error in the same execution flow. If the framework that digest it automatically validates the configuration, its equivilant to the IOC approach with huge constructors as the bean is instantiated when its specifically required. So that&amp;#39;s my reasoning! :)&lt;/p&gt;
&lt;p&gt;You&amp;#39;re idea would be a nice asset, but having seen the horrific abuses proposed for cramming garbage into Java&amp;#39;s type system I&amp;#39;m overly cautious of any type system propsals.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1547364" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1546532</link><pubDate>Tue, 18 Mar 2008 10:37:05 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1546532</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Ben: &amp;quot;The cases I have had them are configuration, where I use setters to avoid the ugliness of a long constructor.&amp;quot;&lt;/p&gt;
&lt;p&gt;Exactly - that's precisely why I've mentioned IoC containers a couple of times. I don't like deep cloning and then explicit validation in terms of design - it would be nicer for the validation to happen automatically on construction, and for there to be no need to do cloning at all.&lt;/p&gt;
&lt;p&gt;I don't see explicit validation and cloning as good design - I see it as a way of getting round the lack of simple initialization of immutable types.&lt;/p&gt;
&lt;p&gt;Not sure what you mean about the xml-style type system and supportive framework, but even if there's a lot of generated code for production stuff, I think it's important to be able to easily construct objects manually for unit tests and the like.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1546532" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1546511</link><pubDate>Tue, 18 Mar 2008 09:32:22 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1546511</guid><dc:creator>Ben</dc:creator><description>&lt;p&gt;Its pretty rare that I have an immutable object with a large number of attributed. If you have any class with a lot, you probably need to revisit the design.&lt;/p&gt;
&lt;p&gt;The cases I have had them are configuration, where I use setters to avoid the ugliness of a long constructor. Example - a distributed master/worker framework, where configuration is set before the task&amp;#39;s execution. In these cases, I&amp;#39;ve cloned the object (deep copy) so that the client can&amp;#39;t modify it. I&amp;#39;ll also add a validation method before execution starts.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;re concern is objects like a user&amp;#39;s profile, as in your example, it really isn&amp;#39;t a big deal once you have an xml-style type system and supportive framework. Its all generated code, such that the mess of large constructors isn&amp;#39;t seen.&lt;/p&gt;
&lt;p&gt;Anyway, the point is that good design resolves all these issues. Everyone seems to want to put their &amp;#39;touch&amp;#39; on their favorite language and add something new. If you focus on a clean design even the most complex tasks really becomes simple and elegant. At that point, you don&amp;#39;t need the compiler or language to do anything but get faster.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1546511" width="1" height="1"&gt;</description></item><item><title>Dew Drop - March 17, 2008 | Alvin Ashcraft's Morning Dew</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1545970</link><pubDate>Mon, 17 Mar 2008 12:07:31 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1545970</guid><dc:creator>Dew Drop - March 17, 2008 | Alvin Ashcraft's Morning Dew</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Dew Drop - March 17, 2008 | Alvin Ashcraft&amp;#39;s Morning Dew&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1545970" width="1" height="1"&gt;</description></item><item><title>Reflective Perspective - Chris Alcock  &amp;raquo; The Morning Brew #54</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1545623</link><pubDate>Mon, 17 Mar 2008 08:24:08 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1545623</guid><dc:creator>Reflective Perspective - Chris Alcock  » The Morning Brew #54</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Reflective Perspective - Chris Alcock &amp;nbsp;&amp;amp;raquo; The Morning Brew #54&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1545623" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1544521</link><pubDate>Sun, 16 Mar 2008 19:11:18 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1544521</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Restricting the defaults that far would severely limit the usefulness of the feature, IMO. I'm sure it wouldn't be that hard to include defaults in a useful manner.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1544521" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1544477</link><pubDate>Sun, 16 Mar 2008 17:00:10 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1544477</guid><dc:creator>Jonathan Allen</dc:creator><description>&lt;p&gt;Why not use optional parameters and require the defaults to be Null/0. That way their is no default to speak of.&lt;/p&gt;
&lt;p&gt;You could even go one step further and require all optional types to be Reference or Nullable&amp;lt;T&amp;gt;.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1544477" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1544363</link><pubDate>Sun, 16 Mar 2008 10:29:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1544363</guid><dc:creator>Marc Gravell</dc:creator><description>&lt;p&gt;Re defaults... perhaps simply insist that *all* initonly values must be provided in the initializer? A half-formed immutable object isn&amp;#39;t that much use... &amp;nbsp;I think this would address the 90% case... for the remaining case when there are scenarios to use different sets of options, then this &amp;quot;initonly&amp;quot; isn&amp;#39;t suitable, and the author must write ctors for each valid combination.&lt;/p&gt;
&lt;p&gt;But a simple &amp;quot;you can set all, by name, w/o writing a ctor&amp;quot; would go a long way...&lt;/p&gt;
&lt;p&gt;In fact, in this case the *final* ctors probably *replace* the existing ones - i.e. Foo(string) becomes Foo(string,[...]), but there is no Foo(string).&lt;/p&gt;
&lt;p&gt;Of course, at this point all we&amp;#39;ve done is provide &amp;quot;pass by name&amp;quot; to ctors, and an auto-generated ctor... not sure...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1544363" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1544357</link><pubDate>Sun, 16 Mar 2008 10:02:56 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1544357</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Marc, I was thinking about that. The difference would basically be in terms of named vs positional arguments. The defaults issue goes back to &amp;quot;provided by the caller&amp;quot; or &amp;quot;provided by the callee&amp;quot; issue - the same reason Anders didn't want optional parameters in the first place. If the value could be moved to the callee somehow, with the caller indicating which values they're really providing, that would be an alternative approach.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1544357" width="1" height="1"&gt;</description></item><item><title>re: C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1544350</link><pubDate>Sun, 16 Mar 2008 09:51:28 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1544350</guid><dc:creator>Marc Gravell</dc:creator><description>&lt;p&gt;Another thought; one of the outstnadning issues was default values. If that can be solved, what difference between a compiler-generated init class, and a compiler-generated constructor? i.e. those members marked &amp;quot;initonly&amp;quot; actually become an additional (single) ctor, with the defaults being provided by the compiler. For every standard public/protected ctor(foo,bar), there might be an additional ctor(foo,bar,[the various initonly members)...&lt;/p&gt;
&lt;p&gt;At the language level, these additional ctors could perhaps be attributed and available by member-name via initializers - but from other (unaware) languauges could be used as the &amp;quot;lots of parameters&amp;quot; ctor.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1544350" width="1" height="1"&gt;</description></item><item><title>C# 4 et automatic properties</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#1544309</link><pubDate>Sun, 16 Mar 2008 08:07:20 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1544309</guid><dc:creator>Matthieu MEZIL</dc:creator><description>&lt;p&gt;Jon Skeet pr&amp;#233;sente dans son dernier billet une future nouveaut&amp;#233; de C#4 : public string Line1 { get; initonly&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1544309" width="1" height="1"&gt;</description></item></channel></rss>