您好,登錄后才能下訂單哦!
System.Collections命名空間中的枚舉器接口(IEnumerator)
***attention field : object Current{get;}
***interface method: bool MoveNext()
***interface method: void Reset();
using System;
using System.Runtime.InteropServices;
namespace System.Collections
{
// Summary:
// Supports a simple iteration over a nongeneric collection.
[ComVisible(true)]
[Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerator
{
// Summary:
// Gets the current element in the collection.
//
// Returns:
// The current element in the collection.
//
// Exceptions:
// System.InvalidOperationException:
// The enumerator is positioned before the first element of the collection or
// after the last element.
object Current { get; }
// Summary:
// Advances the enumerator to the next element of the collection.
//
// Returns:
// true if the enumerator was successfully advanced to the next element; false
// if the enumerator has passed the end of the collection.
//
// Exceptions:
// System.InvalidOperationException:
// The collection was modified after the enumerator was created.
bool MoveNext();
//
// Summary:
// Sets the enumerator to its initial position, which is before the first element
// in the collection.
//
// Exceptions:
// System.InvalidOperationException:
// The collection was modified after the enumerator was created.
void Reset();
}
}
2.System.Collections命名空間中的可枚舉類型接口(IEnumerable)
***interface method: GetEnumerator()
using System.Runtime.InteropServices;
namespace System.Collections
{
// Summary:
// Exposes the enumerator, which supports a simple iteration over a non-generic
// collection.
[ComVisible(true)]
[Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerable
{
// Summary:
// Returns an enumerator that iterates through a collection.
//
// Returns:
// An System.Collections.IEnumerator object that can be used to iterate through
// the collection.
[DispId(-4)]
IEnumerator GetEnumerator();
}
}
3.System.Collections命名空間中的集合接口(ICollection)
***field: int Count{get;}
***field: bool isSynchronized{get;}
***field: object SyncRoot{get;}//同步根
***inteface method: CopyTo(Array array,int index);//attention there is a argument:Array
using System;
using System.Runtime.InteropServices;
namespace System.Collections
{
// Summary:
// Defines size, enumerators, and synchronization methods for all nongeneric
// collections.
[ComVisible(true)]
public interface ICollection : IEnumerable
{
// Summary:
// Gets the number of elements contained in the System.Collections.ICollection.
//
// Returns:
// The number of elements contained in the System.Collections.ICollection.
int Count { get; }
//
// Summary:
// Gets a value indicating whether access to the System.Collections.ICollection
// is synchronized (thread safe).
//
// Returns:
// true if access to the System.Collections.ICollection is synchronized (thread
// safe); otherwise, false.
bool IsSynchronized { get; }
//
// Summary:
// Gets an object that can be used to synchronize access to the System.Collections.ICollection.
//
// Returns:
// An object that can be used to synchronize access to the System.Collections.ICollection.
object SyncRoot { get; }
// Summary:
// Copies the elements of the System.Collections.ICollection to an System.Array,
// starting at a particular System.Array index.
//
// Parameters:
// array:
// The one-dimensional System.Array that is the destination of the elements
// copied from System.Collections.ICollection. The System.Array must have zero-based
// indexing.
//
// index:
// The zero-based index in array at which copying begins.
//
// Exceptions:
// System.ArgumentNullException:
// array is null.
//
// System.ArgumentOutOfRangeException:
// index is less than zero.
//
// System.ArgumentException:
// array is multidimensional.-or- The number of elements in the source System.Collections.ICollection
// is greater than the available space from index to the end of the destination
// array.
//
// System.ArgumentException:
// The type of the source System.Collections.ICollection cannot be cast automatically
// to the type of the destination array.
void CopyTo(Array array, int index);
}
}
4.System.Collections命名空間中的列表接口(IList)
***field: bool IsFixedSize{get;}
***field: bool IsReadOnly{get;}
***indexer: object this[int index]{get;set;}//索引器 (indexer)是這樣一個成員:它支持按照索引數組的方法來索引對象。索引器的聲明與屬性類似,不同的是該成員的名稱是this,后跟一個位于定界符[和]之間的參數列表。在索引器的訪問器中可以使用這些參數。與屬性類似,索引器可以是讀寫、只讀和只寫的,并且索引器的訪問器可以是虛的。
***interface method: int Add(object value);
***interface method: void Clear();
***interface method: bool Contains(object value);
***interface method: int IndexOf(object value);
***interface method:void Insert(int index,object value);
***interface method:void Remove(object value);
***interface method: void RemoveAt(int index);
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace System.Collections
{
// Summary:
// Represents a non-generic collection of objects that can be individually accessed
// by index.
[ComVisible(true)]
public interface IList : ICollection, IEnumerable
{
// Summary:
// Gets a value indicating whether the System.Collections.IList has a fixed
// size.
//
// Returns:
// true if the System.Collections.IList has a fixed size; otherwise, false.
bool IsFixedSize { get; }
//
// Summary:
// Gets a value indicating whether the System.Collections.IList is read-only.
//
// Returns:
// true if the System.Collections.IList is read-only; otherwise, false.
bool IsReadOnly { get; }
// Summary:
// Gets or sets the element at the specified index.
//
// Parameters:
// index:
// The zero-based index of the element to get or set.
//
// Returns:
// The element at the specified index.
//
// Exceptions:
// System.ArgumentOutOfRangeException:
// index is not a valid index in the System.Collections.IList.
//
// System.NotSupportedException:
// The property is set and the System.Collections.IList is read-only.
object this[int index] { get; set; }
// Summary:
// Adds an item to the System.Collections.IList.
//
// Parameters:
// value:
// The object to add to the System.Collections.IList.
//
// Returns:
// The position into which the new element was inserted, or -1 to indicate that
// the item was not inserted into the collection,
//
// Exceptions:
// System.NotSupportedException:
// The System.Collections.IList is read-only.-or- The System.Collections.IList
// has a fixed size.
int Add(object value);
//
// Summary:
// Removes all items from the System.Collections.IList.
//
// Exceptions:
// System.NotSupportedException:
// The System.Collections.IList is read-only.
void Clear();
//
// Summary:
// Determines whether the System.Collections.IList contains a specific value.
//
// Parameters:
// value:
// The object to locate in the System.Collections.IList.
//
// Returns:
// true if the System.Object is found in the System.Collections.IList; otherwise,
// false.
bool Contains(object value);
//
// Summary:
// Determines the index of a specific item in the System.Collections.IList.
//
// Parameters:
// value:
// The object to locate in the System.Collections.IList.
//
// Returns:
// The index of value if found in the list; otherwise, -1.
int IndexOf(object value);
//
// Summary:
// Inserts an item to the System.Collections.IList at the specified index.
//
// Parameters:
// index:
// The zero-based index at which value should be inserted.
//
// value:
// The object to insert into the System.Collections.IList.
//
// Exceptions:
// System.ArgumentOutOfRangeException:
// index is not a valid index in the System.Collections.IList.
//
// System.NotSupportedException:
// The System.Collections.IList is read-only.-or- The System.Collections.IList
// has a fixed size.
//
// System.NullReferenceException:
// value is null reference in the System.Collections.IList.
void Insert(int index, object value);
//
// Summary:
// Removes the first occurrence of a specific object from the System.Collections.IList.
//
// Parameters:
// value:
// The object to remove from the System.Collections.IList.
//
// Exceptions:
// System.NotSupportedException:
// The System.Collections.IList is read-only.-or- The System.Collections.IList
// has a fixed size.
void Remove(object value);
//
// Summary:
// Removes the System.Collections.IList item at the specified index.
//
// Parameters:
// index:
// The zero-based index of the item to remove.
//
// Exceptions:
// System.ArgumentOutOfRangeException:
// index is not a valid index in the System.Collections.IList.
//
// System.NotSupportedException:
// The System.Collections.IList is read-only.-or- The System.Collections.IList
// has a fixed size.
void RemoveAt(int index);
}
}
5.介紹一種實現了以上其中三個接口的抽象類(CollectionBase)
掃盲:如果一個類不與具體的事物相聯系,而只是表達一種抽象的概念,僅僅是作為其派生類的一個基類,這樣的類就是抽象類,在抽象類中聲明方法時,如果加上abstract時就是抽象方法。
抽象類與非抽象類的主要區別:
·抽象類不能直接被實例化
·抽象類中可以包含抽象成員,但非抽象類中不可以
·抽象類不能被密封
抽象方法概述及聲明
聲明抽象方法時需注意:·抽象方法必須聲明在抽象類中 ·聲明抽象方法時,不能使用virtual、static、private修飾符。
在抽象類中抽象方法不提供實現。
抽象類和接口的區別:
·它們的派生類只能繼承一個基類,即只能繼承一個抽象類,但是可以繼承多個接口。
·抽象類中可以定義成員的實現,但接口中不可以。
·抽象類中包含字段、構造函數、析構函數、靜態成員或常量等,接口中不可以。
·抽象類中的成員可以私有的(只要不是抽象的)、受保護的、內部的或受保護的內部成員,但接口中的成員必須是公共的。
PS:抽象類和接口這兩種類型用于完全不同的目的。抽象類主要用作對象系列的基類,共享某些主要特性,例如共同的目的和結構。接口則主要用于類,這些類在基礎水平上有所不同,但仍然可以完成某些相同的任務。
***construction method: protected CollectionBase();
***construction method: protected CollectionBase();
***normal field:public int Capacity{get;set;}
***normal field: public int Count{get;}
***normal field: protected ArrayList InnerList{get;}//attention return value type:ArrayList
***normal field:protected IList List{get;}// attention return value type:IList
***normal method:public void Clear();
***normal method:public IEnumerator GetEnumerator();//attention return value type IEnumerator
***normal method: protected virtual void OnClear();
***normal method:protected virtual void OnClearComplete();
***normal method:protected virtual void OnInsert(int index,object value);
***normal method:protected virtual void OnInsertComplete(int index,object value);
***normal method:protected virtual void OnRemove(int index,object value);
***normal method:protected virtual void OnRemoveComplete(int index,object value);
***normal method:protected virtual void OnSet(int index,object oldValue,object newValue);
***normal method:protected virtual void OnSetComplete(int index,object oldValue,object newValue);
***normal method:protected virtual void OnValidate(object value);
***normal method:public void RemoveAt(int index);
using System;
using System.Runtime.InteropServices;
namespace System.Collections
{
// Summary:
// Provides the abstract base class for a strongly typed collection.
[Serializable]
[ComVisible(true)]
public abstract class CollectionBase : IList, ICollection, IEnumerable
{
// Summary:
// Initializes a new instance of the System.Collections.CollectionBase class
// with the default initial capacity.
protected CollectionBase();
//
// Summary:
// Initializes a new instance of the System.Collections.CollectionBase class
// with the specified capacity.
//
// Parameters:
// capacity:
// The number of elements that the new list can initially store.
protected CollectionBase(int capacity);
// Summary:
// Gets or sets the number of elements that the System.Collections.CollectionBase
// can contain.
//
// Returns:
// The number of elements that the System.Collections.CollectionBase can contain.
//
// Exceptions:
// System.ArgumentOutOfRangeException:
// System.Collections.CollectionBase.Capacity is set to a value that is less
// than System.Collections.CollectionBase.Count.
//
// System.OutOfMemoryException:
// There is not enough memory available on the system.
[ComVisible(false)]
public int Capacity { get; set; }
//
// Summary:
// Gets the number of elements contained in the System.Collections.CollectionBase
// instance. This property cannot be overridden.
//
// Returns:
// The number of elements contained in the System.Collections.CollectionBase
// instance.Retrieving the value of this property is an O(1) operation.
public int Count { get; }
//
// Summary:
// Gets an System.Collections.ArrayList containing the list of elements in the
// System.Collections.CollectionBase instance.
//
// Returns:
// An System.Collections.ArrayList representing the System.Collections.CollectionBase
// instance itself.Retrieving the value of this property is an O(1) operation.
protected ArrayList InnerList { get; }
//
// Summary:
// Gets an System.Collections.IList containing the list of elements in the System.Collections.CollectionBase
// instance.
//
// Returns:
// An System.Collections.IList representing the System.Collections.CollectionBase
// instance itself.
protected IList List { get; }
// Summary:
// Removes all objects from the System.Collections.CollectionBase instance.
// This method cannot be overridden.
public void Clear();
//
// Summary:
// Returns an enumerator that iterates through the System.Collections.CollectionBase
// instance.
//
// Returns:
// An System.Collections.IEnumerator for the System.Collections.CollectionBase
// instance.
public IEnumerator GetEnumerator();
//
// Summary:
// Performs additional custom processes when clearing the contents of the System.Collections.CollectionBase
// instance.
protected virtual void OnClear();
//
// Summary:
// Performs additional custom processes after clearing the contents of the System.Collections.CollectionBase
// instance.
protected virtual void OnClearComplete();
//
// Summary:
// Performs additional custom processes before inserting a new element into
// the System.Collections.CollectionBase instance.
//
// Parameters:
// index:
// The zero-based index at which to insert value.
//
// value:
// The new value of the element at index.
protected virtual void OnInsert(int index, object value);
//
// Summary:
// Performs additional custom processes after inserting a new element into the
// System.Collections.CollectionBase instance.
//
// Parameters:
// index:
// The zero-based index at which to insert value.
//
// value:
// The new value of the element at index.
protected virtual void OnInsertComplete(int index, object value);
//
// Summary:
// Performs additional custom processes when removing an element from the System.Collections.CollectionBase
// instance.
//
// Parameters:
// index:
// The zero-based index at which value can be found.
//
// value:
// The value of the element to remove from index.
protected virtual void OnRemove(int index, object value);
//
// Summary:
// Performs additional custom processes after removing an element from the System.Collections.CollectionBase
// instance.
//
// Parameters:
// index:
// The zero-based index at which value can be found.
//
// value:
// The value of the element to remove from index.
protected virtual void OnRemoveComplete(int index, object value);
//
// Summary:
// Performs additional custom processes before setting a value in the System.Collections.CollectionBase
// instance.
//
// Parameters:
// index:
// The zero-based index at which oldValue can be found.
//
// oldValue:
// The value to replace with newValue.
//
// newValue:
// The new value of the element at index.
protected virtual void OnSet(int index, object oldValue, object newValue);
//
// Summary:
// Performs additional custom processes after setting a value in the System.Collections.CollectionBase
// instance.
//
// Parameters:
// index:
// The zero-based index at which oldValue can be found.
//
// oldValue:
// The value to replace with newValue.
//
// newValue:
// The new value of the element at index.
protected virtual void OnSetComplete(int index, object oldValue, object newValue);
//
// Summary:
// Performs additional custom processes when validating a value.
//
// Parameters:
// value:
// The object to validate.
//
// Exceptions:
// System.ArgumentNullException:
// value is null.
protected virtual void OnValidate(object value);
//
// Summary:
// Removes the element at the specified index of the System.Collections.CollectionBase
// instance. This method is not overridable.
//
// Parameters:
// index:
// The zero-based index of the element to remove.
//
// Exceptions:
// System.ArgumentOutOfRangeException:
// index is less than zero.-or-index is equal to or greater than System.Collections.CollectionBase.Count.
public void RemoveAt(int index);
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。