Writing immutable code in C# 6.0

More than month ago I had written two articles about immutable object in C#. Several days ago I have read article about Easier Immutable Objects in C# 6 and VB 12. How could look my example in C# 6.0? Since C# 6.0 is available as draft, following code is only possible syntax.
Declaration of immutable data class SupportCase:

public record class SupportCase(string title: Title, Customer customer: Customer, SupportCaseStatus status: Status = SupportCaseStatus.New, IList<string> descriptions: Description = null);

Using objects of immutable SupportCase class:

    ...
    SupportCase sc1 = new SupportCase("Origin Title", cust);
    SupportCase sc2 = new SupportCase(title: "Origin Title", customer: cust, status: SupportCaseStatus.Open);
    SupportCase sc3 = new SupportCase(title: "Origin Title", customer: cust, descriptions: new List<string>(new string[] {"Description 1", "Description 2"} ));
    ...
    SupportCase sc4 = sc1 with Status: SupportCaseStatus.Open;
    SupportCase sc5 = sc2 with Descriptions: sc3.Descriptions;
    ...

I am looking forward, how these immutable objects will be possible to write in C# 6.0.

That’s all, now go write some code.

Leave a Reply

Your email address will not be published. Required fields are marked *