answersLogoWhite

0

  1. As to access a data member of an object or class (a class can have property, or static property).
  2. A property may have different accessing modifiers (new in .net 2.0) separately for the getters and setters (the mutators)
  3. .Net framework itself uses and depends on Property feature heavily.

One of the rookie mistakes or being a lazy developer is to allow the data members being directly accessed (get and set the values) as a public field:

public class Person { public string Name; }

vs

public class BetterPersonImplementation {

public string Name {

get;

set;

}

} //the new way to declare a data member via property declaration

The first benefit of moving from direct access to Property implementation is now your class can be directly apply to a window forms (direct field access cannot!!)

The hidden benefit - you are following the object principles - encapsulation

You are denying the public to directly playing with aPerson's Name. (If it were a body part.... yak)

The other benefit: if when setting the name value, let's assume that it should never be set to "Name", and there are more than 10 assignments in your code assignements aPerson.Name = ....

You would have to insert the check "if the value != "Name" at 10 different places.

Worse yet, someone else uses your class in their codes, you have to notify them as well.... (well, you won't make this kind of mistake if someone is going to use your code in the first place, right?)

By using Property feature, you have only 1 place to make the change, while the 10 assignments (the property setter) remain the same!

User Avatar

Wiki User

13y ago

What else can I help you with?