What does this do in C#?
I'm newbie at C# and coul开发者_开发问答dn't find anything about this:
public bool HasUPedidos { get { return upedidos > 0; } }
What does this expression does? Thank you.
It's called a property. That particular one will return true if upedidos is greater than 0, false if not.
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx
It's an Property that returns true if upedidos is greater that 0.
public
is the visibility (in this case it can be called by things outside the class
bool
is the return type (boolean can contain either true or false)
HasUPedidos
is the name of the property
{ get { return upedidos > 0; } }
this is the get/set methods, in this case only a get. Instead of being bound to a private boolean value this tests to see if we have a value greater than 0 in the variable upedidos
and returns the result of that test (true or false)
It will return true if upedidos
is greater than 0.
upedidos > 0
evaluates to a boolean and reading the value of the HasUPedidos
property will get that boolean.
This is a public property with name HasUPedios and a return type bool. This will evaluate the expression upedidos > 0
and return its value.
You can learn about properties in more details at: http://msdn.microsoft.com/en-us/library/aa288470%28v=vs.71%29.aspx
It is a public boolean that returns whether the local variable upedidos ( which I presume is a counter of csome sort ) is greater than 0.
Whats the issue?
You're creating a property that only returns, it returns a true/false bool value. True if upedidos is greater than 0.
This is property which returns boolean
value, i.e. true
if upedidos
> 0 otherwise false
It's a property as others have mentioned, similar to a method like this:
public bool getHasUPedidos() {
if (upepidos > 0)
return true;
else
return false;
}
Like other admitted this is a property that has only get accessory. Under the hood. It will convert by the compiler to a method like:
bool get_HasUPedidos() { return upedidos > 0; }
if it has a set accessor like:
HasUPedidos
{
get { return upedidos > 0; }
set { upedidos = value; }
}
then it will have another method
bool get_HasUPedidos() { return upedidos > 0; }
bool set_HasUPedidos(bool value) { upedidos = value; }
That will return true
if upedidos
is above zero in value or false
otherwise
精彩评论