properties and fields

  • Thread starter Thread starter Cyrus Chiu
  • Start date Start date
C

Cyrus Chiu

I want to ask what is the difference of the usage between properties and
fields in .net?
what is the advantage of accessing properties rather than just access
fields?
also if a object want to access it's own data member in it's own method,
which one should be accessed (properties/fields) ?
thz
 
You can do checks in a property acessor and or call external DLL functions
to set values. like maybe the prop set could call QueryPerformanceCounter
for a high res perf counter like..

get
{
return value;
}
set
{
QueryPerformanceCounter(ref l);
this.blah = l;
}

You cant do that in a field can u :D
 
ok too early in the morning it should read

get { return this.someField }

Meh, pass the Jolt.
 
Property Wrappers shield the mechanics of your implementation. In a simple
case, there's no significant difference between properties and same scoped
fields - at the time that you write the field.

However, if down the road you realize you need to call a function or perform
some other action that updates related state information, you'll wish you
had used a property instead of a same scope field.
 
A good example of this is a hi res performance counter

public PerfCounterNow
{
get
{
QueryPerformanceCounter(ref this.someInternalField);
return this.someInternalField;
}

}
 
* "Cyrus Chiu said:
I want to ask what is the difference of the usage between properties and
fields in .net?

You can add some validation code when the property is set and do some
other operations when getting or setting the value (for example updating
a counter, ...
 
Back
Top