C# Properties or GET/SET methods ?

Started by
17 comments, last by Washu 18 years, 9 months ago
While reading about C# I came upon these Properties that seem to me like they are a simple way of hiding the get/set methods to access and modify variables inside a class and I wanted to know if they had any real advantages over the classical method. so would it be better to do this

private string foreName;
public string ForeName
{
get
{
return foreName;
}
set
{
if (value.Length > 20)
// code here to take error recovery action
// (eg. throw an exception)
else
foreName = value;
}
}

OR This

private string foreName;

public void getForeName()
{
   return foreName;
}

public void setForeName(String name)
{
   foreName = name;//with more code if needed
}

Thanks for your reply !!
Advertisement
Use the first method. Properties are pretty standard throughout C#.
- I don't pretend to know anything.Snowsoft
I think the C# way is mainly for cleanliness. The traditional way, you end up exposing two methods (but it's kinda nice if you like all your gets and sets listed together in the IDE).
So basically, would it be correct to assume that these properties were brought in for cleanliness and that either methods will provide the exact same results simply displayed differently ?
Quote:Original post by Avenyr
So basically, would it be correct to assume that these properties were brought in for cleanliness and that either methods will provide the exact same results simply displayed differently ?

Not quite. If you ever want to integrate editing of your object into the IDE, you will want everything to use properties instead of seperate get/set functions. The properties grid will only display properties, and can't work with methods.
Turring Machines are better than C++ any day ^_~
According to C# and the .NET Platform it is a better idea to use properties instead of get/set methods as properties integrate better with the .NET base class libraries. It is also mentioned in the book that properties are actually mapped to get and set functions once they are compiled. Another point touched upon is that it is much more syntactically appealing to use properties with the increment/deincrement operators than it is to use said operators with get/set methods.
Thanks everyone for replying it has been really helpful so I will go on and use Properties for functionality with VS.NET

But what about C++ or Java ? I must make use of the GET/SET methods because the properties do not exist for those languages I assume.

This is true but I like to do things like:

class CFoo{private:  int m_x, m_y;public:  int& X() { return m_x; }  int& Y() { return m_y; }};


So then I can do things like:

CFoo foo;foo.X() = 10;foo.Y() = 20;cout<<foo.X();cout<<foo.Y();


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



- 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.
And what's the benefit over public member variables now?
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.


Using the C# way, it would look like:
public class Foo{    private Point point;    public point Location    {        get { return point; }        set { point = value; }    }}Console.Writeline("Location is: ", fooInstance.Location);


Works perfect, and I love it more than having to add () all the time. Also, if you write your own control, using properties allows the developer to change the behaviour in the forms editor, instead in code.

And anon is right, the code you showed begs to make the X/Y variables public, as returning int& from a function gives you full-access to them, begging to be messed up during runtime.

Also, you use properties because if you need sanity checking later, you edit the property and the usage stays the same.

Toolmaker

This topic is closed to new replies.

Advertisement