what this code do in c#
if (!condition)
开发者_如何学编程 return objecttoreturn;
{
//some other code here
}
The code will return objecttoreturn
if the condition is not true
The code inside the brackets {}
will be invoked otherwise. The brackets do not add any value except that any variables declared inside them cannot be used in the rest of the method.
This code is equal to:
if (!condition)
{
return objecttoreturn;
}
else
{
//some other code here
}
There is no need for the else because it will not reach it there except if (!condition) not satisfied. And braces are just for informing there is another scope to run, also usable to collapse it (which can also be done by region).
in other words
public object method() {
if(condition == false)
return objectToReturn;
{
//Block of code, developer can create a block to enclose some code to be more readable or to create block declaration of fields
var a = "Field";
}
// the a is not available here
return null;
}
The code can be correctly written as
if (!condition)
return objecttoreturn;
//some other code here
If the value of condition is true
then objecttoreturn
is returned otherwise some other code is executed
精彩评论