Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Given the simpler definition, the fewer lines of code, the less chance for developer error and the ease with which another developer can understand the code (especially with examples that are more true to life than our simple example), is the value of creating proper read-only, immutable objects, worth the cost? </p> </blockquote> <p>Yes.</p> <p>I note that nothing is stopping you from saying</p> <pre><code>int Foo { get; private set; } </code></pre> <p>and then using the setter only in the constructor, to guarantee immutability of the property. You just don't get the nice object initializer syntax, but you could create a constructor easily enough.</p> <blockquote> <p>Also, do other people believe that making immutable objects in C# should be made easier with an automatic-esq syntax...</p> </blockquote> <p>Yes. In particular, the C# language design team believes that.</p> <p>C# 3.0 added a number of features that make C# a more "immutable-friendly" language: anonymous types, query comprehensions and expression trees in particular encourage an immutable-data "functional" style of programming. It also added features that make C# a more "mutable-friendly" language: automatic properties, object initializers and type-inferred arrays come to mind.</p> <p>The language design team is well aware that these latter features have made it easier to create mutable objects at the same time as the DLR team was making a huge library of immutable expression tree types. The irony was not lost on them, I can assure you.</p> <p>There are many proposals floating around the language design team for better syntactic sugars for making immutable objects. The fact that object initializers, which require mutability, have almost identical syntax as anonymous type initializers, which are immutable, is an obvious starting point for exploration, but not the only one.</p> <p>All that said, of course, first, I no longer speak on behalf of the language design team, and second, there is no announced future version of C# beyond the Roslyn project. Thus, speculation as to possible feature sets for hypothetical future versions of the language is just that: speculation on my part. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload