C#學習筆記 ----泛型

來源:互聯網
上載者:User

標籤:style   blog   color   os   使用   io   strong   ar   div   

泛型的優點和缺點:

效能

型別安全

二進位代碼的重用

代碼的擴充

命名規範(字母T作為首碼)

 

建立泛型型別,如下:

public class LinkedListNode<T>{    public LinkedListNode(T value)    {        this.Value = value;    }    public T Value{get;private set;}    public LinkedListNode<T> Next{get;internal set;}    public LinkedListNode<T> Prev{get;internal set;}}public class LinkedList<T>:IEnumerable<T>{    public LinkedListNode<T> First{get;private set;}    public LinkedListNode<T> Last{get;private set;}        public LinkedListNode<T> AddLast(T node)    {        var newNode = new LinkedListNode(node);        if(First == null)        {            First = newNode;            newNode.Prev = Last;            Last = First;        }        else        {            LinklistNode previous = Last;            Last.Next = newNode;            Last = newNode;            Last.Prev = previous;        }        return newNode;    }    public IEnumerator<T> GetEnumerator()    {        LinkedListNode<T> current = First;        while(current != null)        {            yield return current.Value;            current = current.Next;        }    }        IEnumerator IEnumerable.GetEnumerator()    {        return GetEnumerator();    }}

 

泛型類的功能

在泛型中,根據泛型型別是參考型別還是實值型別,泛型default用於將泛型型別初始化為null或0。

 

泛型支援幾種約束機制:

where T:struct 對於結構約束,類型T必須是實值型別

where T:class 類約束指定類型T必須是參考型別

where T:IFoo 指定類型T必須實現介面IFoo

where T:new() 這是一個建構函式約束,指定類型T必須有一個預設建構函式

where T1:T2 這個約束也可以指定,類型T1派生自泛型型別T2。該約束也稱為裸類型約束。

使用泛型型別還可以合并多個約束

 

泛型類的靜態成員只能在類的一個執行個體中共用

 

泛型介面

 

泛型結構

Nullable<T>

結構Nullable<T>定義了一個約束:其中的泛型型別T必須是結構

如下:

public struct Nullable<T>    where T:struct{    public Nullable(T value)    {        this.hasValue = true;        this.value = value;    }    private bool hasValue;    public bool HasValue;    {        get        {            return hasValue;        }    }    private T value;    public T Value;    {        get        {            if(!hasValue)            {                throw new InvalidOperationException("no value");            }            return value;        }    }    public static explicit operator T(Nullable<T> value)    {        return value.Value;    }        public static implicit operator Nullable<T>(T value)    {        return new Nullable<T>(value);    }    public override string ToString()    {        if (!HasValue)            return String.Empty;        return this.value.ToString();    }}

 

定義可空類型的變數,使用“?”

 

泛型方法

C#學習筆記 ----泛型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.