views:

767

answers:

4

In C# you can create getter/setters in a simpler way than other languages:

public int FooBar { get; set; }

This creates an internal private variable which you can't address directly, with the external property 'FooBar' to access it directly.

My question is - how often do you see this abused? It seems like it has a high potential to violate encapsulation best-practices often. Don't get me wrong, I use it as appropriate, and partial variations of it for read-only write-only types of properties, but what are your unpleasant experiences with it from other authors in your code base?

Clarification: the intended definition of abuse would indeed be creating such a property when private variables are appropriate.

+3  A: 

There is no "abuse" in simply not writing the field manually; and it is good to encourage all access via the property (not directly to the field) anyway!

The biggest problem I know of is with binary serialization, where it gets a bit tricky to change back to a regular field without making it version-incompatible - but then... use a different serializer ;-p

It would be nice if there was a "proper" readonly variant, and if you didn't need to use :this() ctor-chaining on structs, but.... meh!

Marc Gravell
I agree. I think Properties and can help extend things later on (especially if they are public and you expect someone else to use your classes).
Ian
Follow the bouncing instruction pointer here ( ;) ): Automatic properties, you can't get to their backing stores. If it's eadonly then there's no way to give it a value *at all*. So thus it would be pointless...
RCIX
@RCIX: The idea would be to declare it as a private set, but use a `[Readonly]` to force this access to be constrained exactly as `readonly` is.
Steven Sudit
@Steven Sudit: i suppose that makes sense...
RCIX
+1  A: 

I haven't seen any abuse of it. And to be honest, I don't really see what you mean, as I can't see how this syntax could be abused.

Razzie
+13  A: 

I've seen it abused (in my opinion). In particular, when the developer would normally write:

private readonly int foo;
public int Foo
{ 
    get 
    { 
        return foo;
    }
}

they'll sometimes write:

public int Foo { get; private set; }

Yes, it's shorter. Yes, from outside the class it has the same appearance - but I don't view these as the same thing, as the latter form allows the property to be set elsewhere in the same class. It also means that there's no warning if the property isn't set in the constructor, and the field isn't readonly for the CLR. These are subtle differences, but just going for the second form because it's simpler and ignoring the differences feels like abuse to me, even if it's minor.

I'm really, really hoping we get a proper "readonly automatically implemented property" some time...

Jon Skeet
Good point. Would also like to see readonly for automatic properties
BengtBe
Interesting point - I'd never thought of that.
ChrisF
I've seen this commonly as well, but your take is interesting.
byte
Its not nearly the same, cause you marked the local variable foo as readonly. If it is readonly, it can't be set anywhere other as on first initialization, if not, it can be accessed from elsewhere in the same class - like the latter form of you.
BeowulfOF
@BeowulfOF - that being the point...
Marc Gravell
I think beowulf's sideways making the point that it's the readonly part which makes this "abusive", which is pretty damned narrow
annakata
@annakata: I see it as an abuse because you're taking a shortcut to represent something which is *like* what you really mean, but isn't *actually* what you really mean.
Jon Skeet
I was also hoping for some way to mark the underlying field readonly, perhaps via an attribute, but it looks like this feature didn't make it to the C# 4 release. Sucks.
Steven Sudit
@Steven: I don't think it was ever part of any candidate set of features. At least, not one that I was privy to :)
Jon Skeet
I'm sure you're right, Jon, but it really ought to have been, because I'm guilty of routinely making exactly this mistake and wish there was an alternative that didn't require an explicit backing field. My conscience troubles me.
Steven Sudit
@Steven: Oh I totally agree. It's my #1 "this is really simple, please make it so" wish for C# 5. The team knows, and I'm hopeful - modulo the normal "even a small feature takes a lot of design, implementation and testing effort" caveat.
Jon Skeet
A: 

I don't think automatic properties are any worse than regular properties in regards to encapsulation.

If you mean that some developers use public automatic properties instead private fields then this is of course wrong and breaks encapsulation..

BengtBe