簡體   English   中英

困惑於? C#中的運算符

[英]Confused with the ? operator in C#

如果full_name的字符串長度> 5,我有一個名為full_name的變量我想將nm設置為full_name的前4個字符,否則我想將nm設置為full_name的所有字符。

var nm;

if (full_name.Length > 5)
{
    nm = full_name.Substring(0, 4);
}
else
{
    nm = full_name;
};

我對“?”感到困惑 運營商。

我可以用它嗎?

var nm = full_name.Length > 5 ? full_name.Substring(0, 4) : full_name;

暫無
暫無

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

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