A: The most oft quoted benefit is as follows: You can start with public member
variables, and at a later stage in your development process if you decide
that you need to do some processing whenever the variable is read or written
you can do so by changing the public member variable into a property.
A: In reality, if you change a public member variable to a property you have
no guarantee that you will not break client code. For example, this code
will not compile if you change Sum to a property:
| public void Test() { Foo(ref bar.Sum); } |
A: Properties have a cleaner syntax because you don't have to supply the parenthesis.
A: Yes, if "cleaner" to you is fewer characters on the screen.
No, if "cleaner" to you is something that appeals to the sense of logic and reason.
It should be noted that Pascal did not require you to supply the parenthesis when
you call a function, if that function takes no parameters. C requires you to supply
the parenthesis even if the function takes no parameters. Many people think C is
cleaner in this regard because it makes logical sense to require the parenthesis.
A: Private member names should begin with a lowercase letter while other member names
should begin with an uppercase letter.
It should be noted that while this is a good convention to follow while writing code,
it is only a convention, and is not enforced by the compiler. Hence it is not a good
idea to rely on this convention while reading code.
Sample code included with the .NET SDK does not always adhere to this
convention (see portal example).
Also, some tools (such as xsd) generate code which has wrong case properties.
A: Quick, can you spot the inefficiency in this code?
| public void Calc(int[] foo) { Sum = 0; for (int i = 0; i < foo.Length; i++) Sum += foo[i]; } |
What you may have missed is that every += involves two function calls. The only
indication of this fact is the uppercase 'S' in Sum (which by the way is a convention
that is frequently not followed), so the inefficiency is not easy to spot.
Now consider this variation of the same code:
| public void Calc(int[] foo) { Sum = 0; for (int i = 0; i < foo.Length; i++) SetSum(GetSum() + foo[i]); } |
Now the inefficiency is glaringly obvious, so you may rewrite it like this:
| public void Calc(int[] foo) { int temp = 0; for (int i = 0; i < foo.Length; i++) temp += foo[i]; SetSum(temp); } | If getting or setting sum is not cheap (now or in the future),
the resulting improvement in performance may be nothing to sneeze at.
Code that uses properties gives you no immediate
feedback on the compactness and efficiency of the code.
A: Quick, can you spot the database access in this code?
| public int CalculateRaise(int percent) { return (salary + bonus + Commission) * percent / 100; }
|
If you missed the uppercase 'C' in Commission you missed the database access.
(Database access in a property getter is
not uncommon.)
Be sure to choose a good font when reading code — one in which uppercase C looks
very different from lowercase c. Better still, rewrite your code like this, so
you can clearly see the function call:
| public int CalculateRaise(int percent) { return (salary + bonus + GetCommission()) * percent / 100; }
|
A: Properties buy very little when weighed against the problems
its use is likely to cause.
C# Best Practice
Discourage use of properties
|
[Back to Index]
|