Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial<noob />
17,062 PointsQuestions about when to use computed properties and expression-bodied members
Hi, Lets take the isNeturalized property for example.
i can write this property in alot of ways:
- with alot of syntatic sugar:
isNeturalized => Health <= 0;
2.i can write it with a computed property using the get method.
public bool isNeutralized
{
get
{
return health <= 0;
}
}
- and i can actually create a specific method for it like this:
public bool isNetrualized()
{
if(health <=0)
{
return true;
}
}
my questions are :
- if we use the first example the complier ignore the "get"? and how excatly i use this method in Main?
- in this example:
public int health {get; private set;} = 2;
we also use a property but now we we use get and set and we set the initial value to 2, this is not a reguler property, it is a intial values property, i just need to be able to know when there is a auto-properties, computed properties, property intial values?
3.regarding auto-properties how exactly I can use them in the main method later to set or get them?
Sorry for alot of questions..
2 Answers
Steven Parker
231,248 PointsHi, I was alerted by your tag. As you pointed out, there are a lot of ways to define properties, but they all work the same way when you use them. The setter is automatically invoked when you make an assignment to the property, and the getter when you access it. For example:
var hesDead = vader.isNeutralized; // this runs the getter on "isNeutralized"
someObject.customProp = 21; // this runs the setter on "customProp", passing it 21
The choice of which way to define them is a matter of convenience and meeting the program needs. You would not use an auto-property, for example, if you need to access the backing variable.
Did that cover your questions?
<noob />
17,062 PointsHi thanks for ur answer. So to use this property i need to create an instance of the class first:
Invader invader = new Invader();
var hesDead = invader.isNeutralized ;
and to set a property, for example the health property?
Invader invader = new Invader();
var hisHealth = invader.Health = 100;
Steven Parker
231,248 PointsClose, but setting a property doesn't involve a variable:
Invader invader = new Invader();
invader.Health = 100;
<noob />
17,062 Points<noob />
17,062 PointsHi thanks for taking ur time to answer my question :] but im not 100% on it yet. Lets take the isNeturalized example again, after i declare this property i go to Main and i assign a variable to it like this?: var invaderDead = [class name].[propety name]? >> this is the template? >> and after that invaderDead holds the property? same thing with a setter?
another question is what is the differences between a method and a property? all i can see is with a getter method for example, i call the getter likle this : [some class].[getSomething] {return theSomething;}.
what in ur opnion is the better way, should i use methods or properties(i have knoweldge in java} and if so can u recommend another resources for training?
Steven Parker
231,248 PointsSteven Parker
231,248 PointsThis isn't quite right:
var invaderDead = [class name].[property name]
. In my example, "vader" would be an instance of the class, not the class name. And "hesDead" would just be true or false, as the property returned. It would not be a reference to the property itself. I'm not quite sure what you were asking with "same thing with a setter?"And from outside the class, a method is like a function, and a property is like a variable.
And the choice to use one or the other is often dictated by the usage:
There are numerous training sources online, and some are free, but I don't have any recent personal experience with them.