I tried writing some C# code to create an init-only property. I was surprised to find that the property could be changed at run-time. Have I misunderstood the idea of immutability? I am using Visual Studio Community 16.9.3.
Sample code.
namespace TonyStachnicki.Windows {
using System.Management.Automation;
[Cmdlet(VerbsCommon.New, "Person")]
public class NewPerson : Cmdlet {
protected override void ProcessRecord() {
var _Person = new Person {
Name = "Jane Dough"
};
WriteObject(_Person);
}
}
public class Person {
public string Name { get; init; }
}
}
Run time example.
PS D:\Users\Tony> ($Person = New-Person)
Name
----
Jane Dough
PS D:\Users\Tony> $Person.Name = "John Smith"
PS D:\Users\Tony> $Person
Name
----
John Smith
PS D:\Users\Tony> $Person.Name = "Jane Jones"
PS D:\Users\Tony> $Person
Name
----
Jane Jones
PS D:\Users\Tony>
The program behaves the same with this Person class.
public class Person {
public string Name {
get { return m_Name; }
init { m_Name = value; }
}
private readonly string m_Name;
}
In this case the readonly modifier is also ignored.
I think most people would be surprised at the effect of the init-only feature.
Your C# code doesn't re-assign the value, so no C# rules were violated here. Whether powershell resects the rules is entirely up to powershell. Note:
From this source : "When the init keyword is used, it restricts a property to only being set by a Constructor or during Nested Object Creation." That means that
var _Person = new Person {
Name = "Jane Dough"
};
is legal (since it uses nested object creation). This is unlike the pre-C# 9.0 case, where you could define the property without a setter. This allows initialization only in the constructor, but not in a nested object creation.
Therefore, if you define your Person
class as:
public class Person {
public string Name { get; }
}
your code would not compile (and you would need to provide a non-default constructor to your Person class to set Name to something)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With