简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM