I have the following attempt at an immutable object:
class MyObject
{
private static int nextId;
public MyObject()
{
_id = ++nextId;
}
private int _id;
public int Id { get { return _id; } }
public string Name { get; private set; }
}
Then, I try to use it like so:
MyObject o1 = new MyObject { Name = "foo" };
But the object initialiser fails because Name's setter is private. Is there a way around this, or do I have to choose between one or the other?