Object and Collection Initializers #csharp

I would like to have a way to declare auto property setters as private, but still be able to use them in object initializers.

Something like this:

class Dog {
    public int Age { get; private set; }
    public string Name { get; private set; }

and then be able to do this:

Dog dog = new Cat { Age = 7, Name = "Mobby" };

but not this:

dog.Age = 11;
Seems not possible... 'internal' doesn't do the right thing too.
So, if I want to use object initializers, I'm forced to open setters for public use, which is not my prefered default approach.
Did I just fall asleep during that class?
Object and Collection Initializers (C# Programming Guide).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: