This project is read-only.

Data Classes

Topics: C# Language Design
Nov 14, 2014 at 3:23 PM
With the new language features removing boilerplate code, I thought that maybe we can help with one of the more repetitive patterns being used: data classes.
It's pretty much the same all the time: I write my properties, create the constructor, and override the Equals + GetHashCode + ToString.
I let Resharper do most of the work but still end up with a lot of repetitive code.
How about having a shorthand notation for data classes?

Here's an example:
public class Person(string Name, int ID, int Age { set; }, 
   string Health { set; } = "Healthy")
 { Equals(Name, ID), ToString("My name is \{Name}") } 
Nov 14, 2014 at 3:46 PM
Check out the following thread and linked proposal doc:

Draft spec for records and pattern-matching in C#

The spec is mostly about the pattern-matching operators but the inclusion of record/data classes is of the core concepts. The feature set for C# 6.0 is effectively closed so I wouldn't expect anything to come of it in the near term.