Basic knowledge of C # basics (Iliest) interface--Generics

Source: Internet
Author: User
For ArrayList, if the Insert value type throws a boxing operation, and the value type is removed, the following

            ArrayList myarraylist = new ArrayList ();            Myarraylist.add (40);//Boxing            Myarraylist.add (80);//Boxing                        Int32 a1 = (Int32) myarraylist[0];//unpacking            Int32 a2 = ( Int32) myarraylist[1];//Unboxing

resulting in a performance drain. For a detailed explanation of the packing, see the next article.
To solve these problems, there are ilist<t> interfaces in C # that support generics, and the detailed code below looks at the same structure as IList, just adding generics.

 <summary>////generic collection class///</summary>//<typeparam name= "T" ></typeparam> public cl        List<t>: Ilist<t>, IList {//<summary>////////</summary> <typeparam name= "T" ></typeparam> public struct enumertor<t>: IEnumerator, Ienumerator&lt ;            t> {//iterate index private int;            The collection object to which the iterator belongs refers to the private list<t> List;                Public enumertor (List<t> container) {this.list = container;            This.index =-1; public void Dispose () {}///<summary>//display C for implementation of IEnumerator                Urrent Properties//</summary> object IEnumerator.Current {get                {return list[index];          }}//<summary>  Implement Ienumerator<t> 's Current property///</summary> public T-current {                get {return list[index]; }}////<summary>///iterators indicate to the next data location///</summary>// <returns></returns> public bool MoveNext () {if (This.index < list.                Count) {++this.index; } return This.index < list.            Count;            } public void Reset () {this.index =-1;        }}///<summary>///To save the array of data, the T type embodies the role of generics.        </summary> private t[] array;        <summary>///The length of the current collection///</summary> private int count; <summary>///default constructor////</summary> public List (): tHis (1) {} public List (int capacity) {if (capacity < 0) {            throw new Exception ("Set initial length cannot be less than 0");            } if (capacity = = 0) {capacity = 1;        } This.array = new t[capacity];            }///<summary>///Collection length//</summary> public int Count {get            {return this.count;            }}///<summary>///Set actual length///</summary> public int Capacity {            get {return this.array.Length;        }}///<summary>//Whether fixed size///</summary> public bool Isfixedsize            {get {return false; }}///<summary>//whether read-only//</summary> public bool IsReadOnly {            get {return false;        }}///<summary>//Can be the same property///</summary> public bool IsSynchronized            {get {return false;            }}///<summary>///Sync object///</summary> public object SyncRoot {            get {return null; }}///<summary>///long enough to reassign an array of sufficient length///</summary>//<returns>&        Lt;/returns> Private t[] Getnewarray () {return new t[(this.array.Length + 1) * 2]; }///<summary>//Implement Ilist<t>add Method///</summary>//<param name= "value            "></param> public void Add (T value) {int newcount = this.count + 1; if (This.array.Length < Newcount) {t[] neWarray = Getnewarray ();                Array.copy (This.array, NewArray, This.count);            This.array = NewArray;            } This.array[this.count] = value;        This.count = Newcount; }///<summary>///Add objects to the end of the collection///</summary>//<param name= "value" ></pa ram>//<returns></returns> int Ilist.add (object value) {(Ilist<t&gt ;) this).            ADD ((T) value);        return this.count-1; }///<summary>//Implement ilist<t> indexer///</summary>//<param name= "index" &            gt;</param>//<returns></returns> public T This[int index] {get {if (Index < 0 | | Index >= this.count) {throw new Argumento                Utofrangeexception ("index");            } return This.array[index];       }     set {if (Index < 0 | | | Index >= this.count) {throw                New ArgumentOutOfRangeException ("index");            } This.array[index] = value; }}///<summary>///display indexers that implement the IList interface///</summary>//<param name= "in            Dex "></param>///<returns></returns> Object Ilist.this[int index] {            get {return (ilist<t>) this) [index];            } set {(ilist<t>) this) [index] = (T) value; }}///<summary>///delete elements from the collection///</summary>//<param name= "index"        ;</param>//<param name= "Count" ></param> public void removerange (int index, int count) {if (Index < 0) {throw new Argumentoutofrangeexception ("index");            } int removeindex = index + count; if (Count < 0 | | Removeindex > This.count) {throw new ArgumentOutOfRangeException ("index"            );            } array.copy (This.array, index + 1, this.array, index + count-1, this.count-removeindex);        This.count-= count; }///<summary>///IndexOf method to implement Ilist<t> interface///</summary>//<param name            = "value" ></param>///<returns></returns> public int IndexOf (T value) {            int index = 0; if (value = = null) {while (Index < this.count) {if (this.                    Array[index] = = null) {return index;                } ++index;          }} else {while (Index < this.count)      {if (value.                    Equals (This.array[index]) {return index;                } ++index;        }} return-1; }///<summary>//Display the IndexOf method to implement the IList interface///</summary>//<param name= "value            "></param>///<returns></returns> int Ilist.indexof (object value) { Return ((ilist<t>) this).        IndexOf ((T) value); }///<summary>//Find the corresponding array entry///</summary>//<param name= "O" ></param&gt        ; <param name= "Compar" ></param>///<returns></returns> public int IndexOf (object            O, IComparer compar) {int index = 0; while (Index < this.count) {if (compar. Compare (This.array[index], o) = = 0) {return INdex;            } ++index;        } return-1; }///<summary>///Implement the Remove method for ilist<t> interface///</summary>//<param name=            "Value" ></param>///<returns></returns> public bool Remove (T value) { int index = this.            IndexOf (value); if (index >= 0) {this.                RemoveRange (index, 1);            return true;        } return false; }///<summary>///Display the Remove method that implements the IList interface, shown here implementation///</summary>//<param name= "Value" ></param> void Ilist.remove (object value) {((ilist<t>) this).        Remove ((T) value); }///<summary>///Remove References to objects from the collection at the specified location///</summary>//<param name= "index" &GT;&L  t;/param> public void RemoveAt (int index) {removerange (index, 1);      }///<summary>///Popup The last element of the collection///</summary>//<returns></retur            Ns> public Object Popback () {Object o = this.array[this.count-1];            RemoveAt (this.count-1);        return o;        }///<summary>///Popup Set first object///</summary>//<returns></returns>            public Object Popfront () {Object o = this.array[0];            RemoveAt (0);        return o; }///<summary>///Implement the Insert method for the Ilist<t> interface///</summary>//<param name= "Index" ></param>//<param name= "value" ></param> public void Insert (int index, T value ) {if (index >= this.count) {throw new ArgumentOutOfRangeException ("Index            ");            } int newcount = This.count + 1; if (This.array.Length < NEWcount) {t[] NewArray = Getnewarray ();                Array.copy (This.array, NewArray, index);                Newarray[index] = value;                Array.copy (This.array, index, NewArray, index + 1, this.count-index);            This.array = NewArray;                } else {array.copy (This.array, index, This.array, index + 1, this.count-index);            This.array[index] = value;        } this.count = Newcount; }///<summary>//Display the Insert method to implement the IList interface///</summary>//<param name= "index"        ></param>//<param name= "value" ></param> void Ilist.insert (int index, object value) {((ilist<t>) this).        Insert (index, (T) value); }///<summary>//contains method to implement Ilist<t> interface///</summary>//<param Nam E= "value" ></param>//<retUrns></returns> public bool Contains (T value) {return this.        IndexOf (value) >= 0; }///<summary>//Show contains method to implement Ilist<t> interface///</summary>//<param N             Ame= "value" ></param>///<returns></returns> bool Ilist.contains (object value) { Return ((ilist<t>) this).        IndexOf ((T) value) >= 0;            }///<summary>//To compress the collection to the actual length///</summary> public void TrimToSize () {                if (This.array.Length > This.count) {t[] NewArray = null;                    if (This.count > 0) {newarray = new T[this.count];                Array.copy (This.array, NewArray, This.count);                } else {NewArray = new t[1];            } This.array = NewArray;      }  }///<summary>//Empty collection//</summary> public void Clear () {        This.count = 0; }///<summary>//GetEnumerator method to implement IEnumerable interface///</summary>//<returns ></returns> public ienumerator<t> GetEnumerator () {enumertor<t> ator = new            Enumertor<t> (this);        return ator; }///<summary>//Show GetEnumerator method to implement IEnumerable interface///</summary>//<retur Ns></returns> IEnumerator Ienumerable.getenumerator () {return (ienumerable<t>) th IS).        GetEnumerator ();  }///<summary>//CopyTo method to implement Icollection<t> interface///</summary>//<param Name= "Array" ></param>///<param name= "index" ></param> public void CopyTo (t[] array, I NT index) {array.coPY (this.array, 0, array, index, this.count); }///<summary>//Display the CopyTo method for implementing the Icollection<t> interface///</summary>//<p Aram Name= "Array" ></param>///<param name= "index" ></param> void ICollection.CopyTo (ARR        ay array, int index) {array.copy (This.array, 0, array, index, this.count); }    }

Call:

static void Main (string[] args)        {            //because an int is already specified, there is no boxing unboxing operation to join the value type.            list<int> tList = new list<int> ();            Tlist.add (+);            Tlist.add (+);            foreach (int n in tList)            {                Console.WriteLine (n);            }            Console.ReadLine ();        }

The above is the basic knowledge of C # basics (Iliest) interface-generic content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.