C# property and ref parameter, why no sugar?

Because you’re passing the result of the indexer, which is really the result of a method call. There’s no guarantee that the indexer property also has a setter, and passing it by ref would lead to a false security on the developer’s part when he thinks that his property is going to be set without the setter being called.

On a more technical level, ref and out pass the memory address of the object passed into them, and to set a property, you have to call the setter, so there’s no guarantee that the property would actually be changed especially when the property type is immutable. ref and out don’t just set the value upon return of the method, they pass the actual memory reference to the object itself.

Leave a Comment