C# property definition
- by Sunny
For C# properties, I can do this:
public class Employee{
public string Name { get; private set; }
public Employee(string name){
Name = name;
}
}
which means that the Name property can be set within the class Employee & can be read publicly.
But, if I want to restrict the set to only within the constructors of the Employee class, I need to do:
public class Employee{
public readonly string Name = String.Empty;
public Employee(string name){
Name = name;
}
}
But, for this case, I had to change the property to a field.
Is there any reason this is not possible/allowed in C#:
public class Employee{
public string Name { get; private readonly set; }
public Employee(string name){
Name = name;
}
}
IMO this will allow us to have properties which can be set only in the constructor & does not require us to change properties to fields...
Thanks!