On Fri, 26 Jan 2007 20:46:08 +0100, "SpongeBob" <an**@sbox.tugraz.at>
wrote in <uT**************@TK2MSFTNGP05.phx.gbl>:
>I am a c++ programmer and quiet new to c sharp.
Unfortunately, i didnt understand what the adventage of a property is.
One thing that is important to understand is that the compiler creates
getters and setters for each property (assuming that both get and set
are specified). So, if you have a class with this property:
public string Foo
{
get { return m_foo; }
set { m_foo = value; }
}
The compiler will actually generate the methods get_Foo and set_Foo in
the assembly. The end result is very similar to creating your own
accessors in C++ instead of using a public member variable. The
advantage is that the syntax is nicer.
There are many advantages over public fields:
1. The get method can automatically calculate the value of the
property based on some internal data.
2. The set method can do all sorts of things, such as call member
methods to reevaluate the instance's state based on the value passed
in.
3. As others have pointed out, you can change the internal
implementation whenever you like without breaking the interface.
4. Using properties makes composition and delegation easy by allowing
you to wrap the composed class's properties in the composing class's,
just like with methods. You can't do this with public fields.
There are lots of other reasons. As Chris Dunaway suggested, check
the Google Groups archives for past discussions.
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research