我以前都是学出c,c++,这个学期开始学c#有点不适应,在编程中遇到些问题,所以自己在网上查了些资料,翻了一下书,写一些总结。

    关于c#中Stack<T>泛型容器:

    《1》stack,是一种数据结构——栈,是一种操作受到限制的线性表,只能在一端插入和删除,FILO(first input Last Output)或LIFO(last input first Output)

      我们不用去管它在编译器中是采用什么样的存储结构。

 

    C#中泛型容器Stack<T>

    《2》泛型容器:泛型类和泛型方法兼复用性、类型安全和高效率于一身,是与之对应的非泛型的类和方法所不及。泛型广泛用于容器(collections)和对容器操作的方法中。

    .NET框架2.0的类库提供一个新的命名空间System.Collections.Generic,其中包含了一些新的基于泛型的容器类。要查找新的泛型容器类(collection classes)的示例代码,

    请参见基础类库中的泛型。当然,你也可以创建自己的泛型类和方法,以提供你自己的泛化的方案和设计模式,这是类型安全且高效的。下面的示例代码以一个简单的泛型链表

    类作为示范。(多数情况下,推荐使用由.NET框架类库提供的List<T>类,而不是创建自己的表。)类型参数T在多处使用,具体类型通常在这些地方来指明表中元素的类型。

  

    《3》Stack<T>在 vs2013中的定义:

     

    
  1 namespace System.Collections.Generic
  2 {
  3     // 摘要: 
  4     //     表示同一任意类型的实例的大小可变的后进先出 (LIFO) 集合。
  5     //
  6     // 类型参数: 
  7     //   T:
  8     //     指定堆栈中的元素的类型。
  9     [Serializable]
 10     [ComVisible(false)]
 11     [DebuggerDisplay("Count = {Count}")]
 12     public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable
 13     {
 14         // 摘要: 
 15         //     初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例为空并且具有默认初始容量。
 16         public Stack();
 17         //
 18         // 摘要: 
 19         //     初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例包含从指定的集合中复制的元素并且其容量足以容纳所复制的元素数。
 20         //
 21         // 参数: 
 22         //   collection:
 23         //     从其中复制元素的集合。
 24         //
 25         // 异常: 
 26         //   System.ArgumentNullException:
 27         //     collection 为 null。
 28         public Stack(IEnumerable<T> collection);
 29         //
 30         // 摘要: 
 31         //     初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例为空并且具有指定的初始容量或默认初始容量(这两个容量中的较大者)。
 32         //
 33         // 参数: 
 34         //   capacity:
 35         //     System.Collections.Generic.Stack<T> 可包含的初始元素数。
 36         //
 37         // 异常: 
 38         //   System.ArgumentOutOfRangeException:
 39         //     capacity 小于零。
 40         public Stack(int capacity);
 41 
 42         // 摘要: 
 43         //     获取 System.Collections.Generic.Stack<T> 中包含的元素数。
 44         //
 45         // 返回结果: 
 46         //     System.Collections.Generic.Stack<T> 中包含的元素数。
 47         public int Count { get; }
 48 
 49         // 摘要: 
 50         //     从 System.Collections.Generic.Stack<T> 中移除所有对象。
 51         public void Clear();
 52         //
 53         // 摘要: 
 54         //     确定某元素是否在 System.Collections.Generic.Stack<T> 中。
 55         //
 56         // 参数: 
 57         //   item:
 58         //     要在 System.Collections.Generic.Stack<T> 中定位的对象。对于引用类型,该值可以为 null。
 59         //
 60         // 返回结果: 
 61         //     如果在 System.Collections.Generic.Stack<T> 中找到 item,则为 true;否则为 false。
 62         public bool Contains(T item);
 63         //
 64         // 摘要: 
 65         //     从指定数组索引开始将 System.Collections.Generic.Stack<T> 复制到现有一维 System.Array 中。
 66         //
 67         // 参数: 
 68         //   array:
 69         //     作为从 System.Collections.Generic.Stack<T> 复制的元素的目标位置的一维 System.Array。System.Array
 70         //     必须具有从零开始的索引。
 71         //
 72         //   arrayIndex:
 73         //     array 中从零开始的索引,将在此处开始复制。
 74         //
 75         // 异常: 
 76         //   System.ArgumentNullException:
 77         //     array 为 null。
 78         //
 79         //   System.ArgumentOutOfRangeException:
 80         //     arrayIndex 小于零。
 81         //
 82         //   System.ArgumentException:
 83         //     源 System.Collections.Generic.Stack<T> 中的元素数目大于从 arrayIndex 到目标 array 末尾之间的可用空间。
 84         public void CopyTo(T[] array, int arrayIndex);
 85         //
 86         // 摘要: 
 87         //     返回 System.Collections.Generic.Stack<T> 的一个枚举数。
 88         //
 89         // 返回结果: 
 90         //     用于 System.Collections.Generic.Stack<T> 的 System.Collections.Generic.Stack<T>.Enumerator。
 91         public Stack<T>.Enumerator GetEnumerator();
 92         //
 93         // 摘要: 
 94         //     返回位于 System.Collections.Generic.Stack<T> 顶部的对象但不将其移除。
 95         //
 96         // 返回结果: 
 97         //     位于 System.Collections.Generic.Stack<T> 顶部的对象。
 98         //
 99         // 异常: 
100         //   System.InvalidOperationException:
101         //     System.Collections.Generic.Stack<T> 为空。
102         public T Peek();
103         //
104         // 摘要: 
105         //     移除并返回位于 System.Collections.Generic.Stack<T> 顶部的对象。
106         //
107         // 返回结果: 
108         //     从 System.Collections.Generic.Stack<T> 的顶部移除的对象。
109         //
110         // 异常: 
111         //   System.InvalidOperationException:
112         //     System.Collections.Generic.Stack<T> 为空。
113         public T Pop();
114         //
115         // 摘要: 
116         //     将对象插入 System.Collections.Generic.Stack<T> 的顶部。
117         //
118         // 参数: 
119         //   item:
120         //     要推入到 System.Collections.Generic.Stack<T> 中的对象。对于引用类型,该值可以为 null。
121         public void Push(T item);
122         //
123         // 摘要: 
124         //     将 System.Collections.Generic.Stack<T> 复制到新数组中。
125         //
126         // 返回结果: 
127         //     新数组,包含 System.Collections.Generic.Stack<T> 的元素的副本。
128         public T[] ToArray();
129         //
130         // 摘要: 
131         //     如果元素数小于当前容量的 90%,将容量设置为 System.Collections.Generic.Stack<T> 中的实际元素数。
132         public void TrimExcess();
133 
134         // 摘要: 
135         //     枚举 System.Collections.Generic.Stack<T> 的元素。
136         [Serializable]
137         public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
138         {
139 
140             // 摘要: 
141             //     获取枚举数当前位置的元素。
142             //
143             // 返回结果: 
144             //     System.Collections.Generic.Stack<T> 中位于枚举数当前位置的元素。
145             //
146             // 异常: 
147             //   System.InvalidOperationException:
148             //     枚举数定位在该集合的第一个元素之前或最后一个元素之后。
149             public T Current { get; }
150 
151             // 摘要: 
152             //     释放由 System.Collections.Generic.Stack<T>.Enumerator 使用的所有资源。
153             public void Dispose();
154             //
155             // 摘要: 
156             //     Advances the enumerator to the next element of the System.Collections.Generic.Stack<T>.
157             //
158             // 返回结果: 
159             //     如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
160             //
161             // 异常: 
162             //   System.InvalidOperationException:
163             //     在创建了枚举数后集合被修改了。
164             public bool MoveNext();
165         }
166     }
167 }
View Code

相关文章: