iT邦幫忙

0

c# 程式中用 大括號是什麼意思, 和沒有用的差別在那

  • 分享至 

  • xImage

在c#程式中用 大括號是什麼意思

 {
 if (forTest) result = @"xxxxx";
 }

沒有用的
if (forTest) result = @"xxxxx";

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
canrong
iT邦新手 3 級 ‧ 2022-06-10 11:57:12
最佳解答

區分程式區塊

{
    string result = null;
    if (true) result = @"xxxxx";
}
Console.WriteLine(result);//error

string result = null;
if (true){ 
    result = @"xxxxx";
}
Console.WriteLine(result);//work
Jason iT邦新手 4 級 ‧ 2022-06-10 13:24:27 檢舉

區分程式區塊是說在{}裏的值傳不出來,區域變數

Jason iT邦新手 4 級 ‧ 2022-06-10 13:28:31 檢舉
string result = SystemFunctions.Request();
{
    if (true) result = @"xxxxx";
}
Console.WriteLine(result);  //work ?
canrong iT邦新手 3 級 ‧ 2022-06-10 16:12:13 檢舉

區域內宣告的變數無法傳出,你寫的這段程式的確result內值將會被覆蓋,因為result是在{}外層宣告的。你可以把全域變數與區域變數看成是相對關係,而依據就是大括號。

我要發表回答

立即登入回答