In C #, any "variable" that has a getter and setter is called a property. A variable does not have getters and setters or what text books say.
My programming instructor made us have getters and setters for almost every variable we created in Java. Even indexing variables, he forced us to use getter and setter if they were declared in a global class scope. I think this may have been excessive, but it made me create getters and setters.
The real thing about getters and setters is that they are more than likely doing more than just setting an internal variable. Most setters are going to do some type of data validation to make sure that the data can be set to a variable. A getter can also check the returned data for some criteria.
If your property is private and your setters and recipients are publicly available, anyone can access your variable and modify it as if they had public access to the actual variable. Thus, in reality, you should check your data to make sure that it is valid or some other data check.
private int myVariable; public int myVariable { get { return myVariable; } set { if (value < 0) { throw new Exception("This is your exception some where else in code"); } myVariable = value;
The above way is to write down the following
private string firstName; public string FirstName { get {
user3376708 Apr 22 '14 at 16:58 2014-04-22 16:58
source share