Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hi Simon,
- I think I can answer your question. Some years ago I bought one of those $50 reference manuals on C# and basically what it says is that a "field" tells us what the variables of a Class are, and the "properties" tell us how those fields work.
- Part of the problem is that, when using Visual Studio, we don't actually see what the underlying C# code is doing ... Visual Studio handles a lot of that for us.
- For instance, using your examples above, you might often see Bob type:
- prop [tab] [tab]
- to generate the code for properties, and get something like your example:
- public int Health {get; set;}
- This is actually a shorthand that VS can understand. The actual underlying code would be:
- public class Game
- {
- private int Health; //defines the field "Health"
- public Game(int health) // a constuctor using one property
- {
- this.Health = health; // matches the field to the property
- }
- public int Health // the code that makes Health work
- {
- get
- {
- return health;
- }
- set
- {
- health = value;
- }
- }
- }
- Hope this helps
- - john
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement