I am going through a tutorial on XNA, and it uses this code:
private int score = 0;
public int Score
{
get { return score; }
set { score = value; }
}
What is the point of using a property? Why not just use public int Score = 0;?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would just use an auto-property here:
as it’s “the same thing with less typing” (but actually perhaps better because it limits who can set the score 😉
Some reasons for properties (over public Member Variables):
Properties allow setting the visibility for the getter and setter individually, as shown above.
Properties can be specified in Interfaces. (You are programming against interfaces … right? 😉
Switching between Properties and Member Variables breaks the ABI (Application Binary Interface: e.g. needs a recompile against). However, an existing Property’s implementation can be re-defined without breaking the ABI.
Breakpoints can be set in Properties. Occasionally very handy.