سؤال

I'd like an immutable class, or rather a class whose properties are initialised (whether they have public/private setters or not), in the constructor:

public class MyClass
{
    public string MyString { get; }

    public MyClass(string myString)
    {
        MyString = myString;
    }
}

Nice and simple, works fine. Is there any syntactic sugar here that will allow me to do this without the constructor based on the naming convention of my properties? Or am I waiting for C# 8's new 'record' feature?

I vaguely recall seeing something around auto property initialisers, but can't find how I'd tackle this using those.

هل كانت مفيدة؟

المحلول

At time of writing (July 2018), you need to wire up the ctor yourself in C#.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى softwareengineering.stackexchange
scroll top