類或方法名后加<>


聲明類型為泛型。

說明傳遞的參數為泛型,而不僅僅是一種類型。
public void aa<T>(T a)
{
Console.WriteLine(a);
}
調用時可寫:
this.aa<int>(5);
this.aa(string)("aaaa");

C#變量類型后面加?是什么意思?

例如:
int? id = null;
string? name = null;
還有如:

public int?[] InsertTest(int? parentId, string name, DateTime? publishTime, decimal? price, bool? isGood, out int? minId)
    {
        // 僅為說明如何做錯誤處理
        if (String.IsNullOrEmpty(name))
            throw new ArgumentException("參數不能是空", "name");
 
        int? id = null;
        int? count = null;
        minId = null;
 
        Singleton<TestTableAdapter>.Instance.InsertTest(parentId, name, publishTime, price, isGood, ref id, ref count, ref minId);
        return new int?[] { id, count };
    }
更多 2
View Code

答:

單問號---用於給變量設初值的時候,給變量(int類型)賦值為null,而不是0!
雙問號---用於判斷並賦值,先判斷當前變量是否為null,如果是就可以賦一個新值,否則跳過!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM