Advertisement
DevUModerator

Untitled

Nov 12th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. Hi Simon,
  2. 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.
  3. 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.
  4. For instance, using your examples above, you might often see Bob type:
  5.  
  6. prop [tab] [tab]
  7.  
  8. to generate the code for properties, and get something like your example:
  9.  
  10. public int Health {get; set;}
  11.  
  12. This is actually a shorthand that VS can understand. The actual underlying code would be:
  13.  
  14. public class Game
  15. {
  16. private int Health; //defines the field "Health"
  17.  
  18.  
  19. public Game(int health) // a constuctor using one property
  20. {
  21. this.Health = health; // matches the field to the property
  22. }
  23.  
  24. public int Health // the code that makes Health work
  25. {
  26. get
  27. {
  28. return health;
  29. }
  30. set
  31. {
  32. health = value;
  33. }
  34. }
  35. }
  36.  
  37. Hope this helps
  38.  
  39. - john
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement