Hi!
I have encountered a small issue when attempting to create getters and setters in C#. I've been following along a course in unity and tried to replicate what the tutor had written in his C# scripts. As far as I can tell, everything is spot on and I can't seem to find what's causing the error -- Visual Studio suggests adding semi-colons after the 'get' and 'set' keywords, but I don't think that's correct course of action. Here is a snippet of the code that causes the error. Perhaps someone could explain to me what I'm doing wrong and give me some helpful pointers as to how I can go about solving this error.
Presume I've declared two private variables in my class: string name and float health.
public string Name()
{
get {
return name;
}
set {
name = value;
}
}
public float Health()
{
get {
return health;
}
set {
health = value;
}
}
When I attempt to compile this code it throws an error: "expecting ;" with Visual Studio highlighting the 'get' and 'set' keyword. Any ideas?
Edit: The workarounds I've found is implementing an accessor and mutator method for each variable (the Java way) - but I was hoping I could take advantage of C#'s get and set clause to simplify my code.