C# Properties or GET/SET methods ?

Started by
17 comments, last by Washu 18 years, 9 months ago
Do use Get___ and Set___ methods when the implementation of either is particularly slow, memory-intensive, or in any other way behaves unexpectedly. By using properties, you imply to the programmer using them that the implementation is reasonably fast (i.e., it doesn't do more than a simple set/get with maybe a little bit of checking, other minor maintainence).
Advertisement
There is no real benefit. It just makes code look readable. I was refering to C++ code, read the post before mine.
- I don't pretend to know anything.Snowsoft
Quote:Original post by Smack0007
This is true but I like to do things like:

*** Source Snippet Removed ***

So then I can do things like:

*** Source Snippet Removed ***

You would only want to do this with member variables that can be edited freely.


Why not simply make x and y public if you're going to let everything access them without restriction like this? What do you gain by doing it this way?
My point is this. You see alot of people do this:
class CFoo{private:  int m_x, m_y;public:  int GetX() { return m_x; }  void SetX(int x) { m_x = x; }  int GetY() { return m_y; }  void SetY(int y) { m_y = y; }};

There is no added benefit to this, but that is the whole idea behind information hiding, blah blah blah. The guy asked if there was anything equivlent to properties in C++ or Java. I said no. Then I offered something similar.
- I don't pretend to know anything.Snowsoft
Quote:Original post by Smack0007
There is no added benefit to this, but that is the whole idea behind information hiding, blah blah blah.

Actually there is a major benefit to doing it that way. By hiding the implementation you are able to change the layer underneath without breaking the interface for the end-user. This will be paramount in 32/64 bit compatibility and also allows much greater flexibility.

It is the same reason that C# uses properties. Basic getter/setters that hide implementation so that you can change it in the future without affecting the interface and/or breaking current implementations.
You should definitely use properties rather than writing get/set methods, or using public members. Internally, when you create a property get/set (actually get_ and set_) methods are created for you; this allows you to use reflection to cleanly fill objects that aren't native to your project (I do this in Persist - http://www.csharpninja.com/persist/default.aspx - to load database data into objects with only an XML file describing the object), allows data binding to work with objects rather than DataSets, and really helps reduce coupling in code. There may be a small performance hit, but from what I've read it is largely optimized away - I've never seen my code slow down with it.

It's worth remembering that you can implement JUST a get{} block (or just a set{} block) to give read-only or write-only properties, and you can do whatever you like in the code. I'm fond of collection properties that auto-initialize themselves on the first access, and properties like Age which only implement a get{} and calculate from date of birth rather than a private age variable (so it is always correct when you read it).

The appeal over public member variables is that you abstract your interface by using properties - if you need to add some logic to a property without affecting the interface, it is very easy to do so (it's not really hard to convert a public member into a property, though, especially with a refactoring tool). The downside is that it is more typing!
i think the most useful thing of properties in C# is that you can use reflection to enumerate them. so you can use the properties even if you dont know the names of the properties. in C++ i think you can only do properties with a microsoft compiler. doing something like this:

#define readwrite(sDataType, sPropertyName) __declspec(property(get=Get##sPropertyName,put=Set##sPropertyName)) sDataType sPropertyName

#define readonly(sDataType, sPropertyName) __declspec(property(get=Get##sPropertyName)) sDataType sPropertyName

#define writeonly(sDataType, sPropertyName) __declspec(property(put=Set##sPropertyName)) sDataType sPropertyName
Quote:Original post by Smack0007
There is no real benefit. It just makes code look readable. I was refering to C++ code, read the post before mine.
IMO this is less readable:

class CFoo{private:  int m_x, m_y;public:  int& X() { return m_x; }  int& Y() { return m_y; }};CFoo foo;foo.X() = 10;foo.Y() = 20;cout<<foo.X();cout<<foo.Y();


than this:

class CFoo{public:  int x, y;};CFoo foo;foo.x = 10;foo.y = 20;cout<<foo.x;cout<<foo.y;


Yet they achieve the same information hiding level (i.e. none)
class Foo {	struct IntProperty {	public:		IntProperty(int& val) : val_(val) {}		operator int const&() { return val_; }		IntProperty const& operator=(int i) { val_ = i; return *this; }	private:		int& val_;	};public:	Foo() : X(x_), Y(y_) {}	IntProperty X;	IntProperty Y;private:	int x_;	int y_;};


[grin]

Although, I would never recommend that you actually do something so...ugly.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement