C# Field vs Property

0

Category : ,

Property is a function call. Field is not a call - just access into a memory of the class.

Properties are more maintainable than fields. Some properties do not have the equivalent field - you need to write some code to set/get them.

Simple example: say you have a car object. It has a mileage and a gallons as fields. You can make a property MPG by dividing these fields. Notice that there is no MPG field inside an object - you do it on the fly by using a property. And that property is read-only - you cannot set it. It can only be changed by changing mileage field or gallons field.

From the other hand - the critical code path (large loops, for example) should avoid using a lot of properties or get the properties once before the loop.

Take a look here:
http://msdn.microsoft.com/en-us/library/w86s7x04(VS.80).aspx

0 comments:

Post a Comment