I am perplexed as in my spare time I've been reading through C# books to become familiar with the language.
I stumbled upon the use of properties; in this context it is in regards to using a getter/setter for a privately declared field in my class.
This is what I have in my code as to keep it simple:
class ClassName
{
private int hWorked = 24;
public int HoursWorked
{
get
{
return hWorked;
}
}
}
Now the book says that:
If I use the short hand version -
public int HoursWorked {get;}
- that it is the same as the above code.
But what I need clarification on is how the shorthand is able to return the hWorked
value without specifying that the hWorked = value
.
In simple terms: How does the HoursWorked
getter know to target my privately declared hWorked
.