Re: Public property or variable ?



Stefan,
In addition to the other comments.

| The last one just has to be faster, or am I wrong?
Your wrong! The JIT compiler has the option to inline the property Get & Set
routines (actually any short method). Which means the Property will execute
exactly the same as a Field! See my response to Dennis on details & a
demonstration.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/highperfmanagedapps.asp

I would be more concerned about what is "correct". The property suggests
better encapsulation, remember Encapsulation is one of the major tenants of
OO.

Properties also play nicer with System.ComponentModel,
System.Windows.Forms.PropertyGrid and COM interop.

I would not code a field or property based on perceived runtime performance.

Remember the 80/20 rule. That is 80% of the execution time of your program
is spent in 20% of your code. I will optimize (worry about performance,
memory consumption) the 20% once that 20% has been identified & proven to be
a performance problem via profiling (CLR Profiler is one profiling tool).

For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware/yetOptimization.pdf



Here are some thoughts on not using properties:

http://blogs.msdn.com/ericgu/archive/2004/04/29/123588.aspx


Hope this helps
Jay


"Stefan De Schepper" <stefan.de.schepper@xxxxxxxxx> wrote in message
news:42dd6a01$0$4281$ba620e4c@xxxxxxxxxxxxxxxxx
| Should I use:
|
| Private m_Name As String
|
| Public Property Name() As String
| Get
| Return m_Name
| End Get
| Set(ByVal Value As String)
| m_Name = Value
| End Set
| End Property
|
| or just
|
| Public Name As String
|
| The last one just has to be faster, or am I wrong?
|
| Stefan
|
|


.



Relevant Pages