I have a Manager
class with two properties as below:
public class Manager()
{
private string _name;
private List<int> _reportingEmployeesIds;
public string Name { get { return _name; }}
public List<int> ReportingEmployeesIds { get {return _reportingEmployeesIds; } }
I am trying to create an instance of the Manager
class as follows
Manager m = new Manager
{
Name = "Dave", // error, expected
ReportingEmployeesIds = {2345, 432, 521} // no compile error - why?
};
The set property is missing from both the properties but the compiler allows setting ReportingEmployeesIds
whereas does not allow setting the Name property (Error: Property or indexer Manager.Name can not be assigned to, it is readonly).
Why is this the case? Why doesn't the compiler complain about the ReportingEmployeesIds
being readonly.