- Home /
Getter & Setter version Difference
What is the difference from these 2 codes?
version 1:
public int Armor {get;set;}
version 2:
int armor;
public int Armor {
get{ return armor; }
set{ armor = value; }
}
I had both tested and they do exactly the same thing. Could I continue using version 1 without any problems, since its less code?
If I set the armor to 4, both versions will have a result of 4.
Answer by DiegoSLTS · Jun 17, 2015 at 03:08 PM
Yes, the first one is a shorthand for the second one. The only difference is that the first one will create a private variable to use for this property and you can't access it, with the second method you can (some times it's usefull to access the variable without using the property).
Your answer
Follow this Question
Related Questions
Getter and Setter in C# 2 Answers
How can I display an object with a script based off an int in a different script? 1 Answer
Get and Set difference 2 Answers
problems with setters and getters. 1 Answer
getter and setter not working 1 Answer