簡體   English   中英

這段代碼在C#中的作用

[英]what this code do in c#

if (!condition)
                    return objecttoreturn;
                {
              //some other code here
                }

如果條件不成立,代碼將返回objecttoreturn

否則將調用方括號{}的代碼。 方括號不添加任何值,除了方括號內聲明的任何變量不能在該方法的其余部分中使用。

此代碼等於:

if (!condition)
{
     return objecttoreturn;
}
else
{
 //some other code here
}

不需要其他條件,因為除非條件不滿足,否則它不會到達那里。 大括號僅用於通知還有另一個范圍要運行,也可以用來折疊它(也可以按區域完成)。

換一種說法

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;

}

該代碼可以正確地寫為

if (!condition)
    return objecttoreturn;
//some other code here

如果condition的值為true則返回objecttoreturn ,否則執行其他代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM