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

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

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

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

    

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

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

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

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

  

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

     

    

 namespace System.Collections.Generic
{
// 摘要:
// 表示同一任意类型的实例的大小可变的后进先出 (LIFO) 集合。
//
// 类型参数:
// T:
// 指定堆栈中的元素的类型。
[Serializable]
[ComVisible(false)]
[DebuggerDisplay("Count = {Count}")]
public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable
{
// 摘要:
// 初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例为空并且具有默认初始容量。
public Stack();
//
// 摘要:
// 初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例包含从指定的集合中复制的元素并且其容量足以容纳所复制的元素数。
//
// 参数:
// collection:
// 从其中复制元素的集合。
//
// 异常:
// System.ArgumentNullException:
// collection 为 null。
public Stack(IEnumerable<T> collection);
//
// 摘要:
// 初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例为空并且具有指定的初始容量或默认初始容量(这两个容量中的较大者)。
//
// 参数:
// capacity:
// System.Collections.Generic.Stack<T> 可包含的初始元素数。
//
// 异常:
// System.ArgumentOutOfRangeException:
// capacity 小于零。
public Stack(int capacity); // 摘要:
// 获取 System.Collections.Generic.Stack<T> 中包含的元素数。
//
// 返回结果:
// System.Collections.Generic.Stack<T> 中包含的元素数。
public int Count { get; } // 摘要:
// 从 System.Collections.Generic.Stack<T> 中移除所有对象。
public void Clear();
//
// 摘要:
// 确定某元素是否在 System.Collections.Generic.Stack<T> 中。
//
// 参数:
// item:
// 要在 System.Collections.Generic.Stack<T> 中定位的对象。对于引用类型,该值可以为 null。
//
// 返回结果:
// 如果在 System.Collections.Generic.Stack<T> 中找到 item,则为 true;否则为 false。
public bool Contains(T item);
//
// 摘要:
// 从指定数组索引开始将 System.Collections.Generic.Stack<T> 复制到现有一维 System.Array 中。
//
// 参数:
// array:
// 作为从 System.Collections.Generic.Stack<T> 复制的元素的目标位置的一维 System.Array。System.Array
// 必须具有从零开始的索引。
//
// arrayIndex:
// array 中从零开始的索引,将在此处开始复制。
//
// 异常:
// System.ArgumentNullException:
// array 为 null。
//
// System.ArgumentOutOfRangeException:
// arrayIndex 小于零。
//
// System.ArgumentException:
// 源 System.Collections.Generic.Stack<T> 中的元素数目大于从 arrayIndex 到目标 array 末尾之间的可用空间。
public void CopyTo(T[] array, int arrayIndex);
//
// 摘要:
// 返回 System.Collections.Generic.Stack<T> 的一个枚举数。
//
// 返回结果:
// 用于 System.Collections.Generic.Stack<T> 的 System.Collections.Generic.Stack<T>.Enumerator。
public Stack<T>.Enumerator GetEnumerator();
//
// 摘要:
// 返回位于 System.Collections.Generic.Stack<T> 顶部的对象但不将其移除。
//
// 返回结果:
// 位于 System.Collections.Generic.Stack<T> 顶部的对象。
//
// 异常:
// System.InvalidOperationException:
// System.Collections.Generic.Stack<T> 为空。
public T Peek();
//
// 摘要:
// 移除并返回位于 System.Collections.Generic.Stack<T> 顶部的对象。
//
// 返回结果:
// 从 System.Collections.Generic.Stack<T> 的顶部移除的对象。
//
// 异常:
// System.InvalidOperationException:
// System.Collections.Generic.Stack<T> 为空。
public T Pop();
//
// 摘要:
// 将对象插入 System.Collections.Generic.Stack<T> 的顶部。
//
// 参数:
// item:
// 要推入到 System.Collections.Generic.Stack<T> 中的对象。对于引用类型,该值可以为 null。
public void Push(T item);
//
// 摘要:
// 将 System.Collections.Generic.Stack<T> 复制到新数组中。
//
// 返回结果:
// 新数组,包含 System.Collections.Generic.Stack<T> 的元素的副本。
public T[] ToArray();
//
// 摘要:
// 如果元素数小于当前容量的 90%,将容量设置为 System.Collections.Generic.Stack<T> 中的实际元素数。
public void TrimExcess(); // 摘要:
// 枚举 System.Collections.Generic.Stack<T> 的元素。
[Serializable]
public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
{ // 摘要:
// 获取枚举数当前位置的元素。
//
// 返回结果:
// System.Collections.Generic.Stack<T> 中位于枚举数当前位置的元素。
//
// 异常:
// System.InvalidOperationException:
// 枚举数定位在该集合的第一个元素之前或最后一个元素之后。
public T Current { get; } // 摘要:
// 释放由 System.Collections.Generic.Stack<T>.Enumerator 使用的所有资源。
public void Dispose();
//
// 摘要:
// Advances the enumerator to the next element of the System.Collections.Generic.Stack<T>.
//
// 返回结果:
// 如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
//
// 异常:
// System.InvalidOperationException:
// 在创建了枚举数后集合被修改了。
public bool MoveNext();
}
}
}

    《4》Stack<T>中方法

 
 
  名称 说明
Clear()

从 Stack<T> 中移除所有对象。

Contains(T)

确定某元素是否在 Stack<T> 中。

CopyTo(T[], Int32)

从特定的数组索引处开始,将 Stack<T> 复制到现有一维 Array

Equals(Object)

确定指定的对象是否等于当前对象。(从 Object 继承。)

Finalize()

在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。(从 Object 继承。)

GetEnumerator()

返回的枚举数 Stack<T>。

GetHashCode()

作为默认哈希函数。(从 Object 继承。)

GetType()

获取当前实例的 Type。(从 Object 继承。)

MemberwiseClone()

创建当前 Object 的浅表副本。(从 Object 继承。)

Peek()

返回 Stack<T> 顶部的对象而无需移除它。

Pop()

移除并返回位于顶部的对象 Stack<T>。

Push(T)

将对象插入 Stack<T> 的顶部。

ToArray()

将 Stack<T> 复制到新数组。

ToString()

返回表示当前对象的字符串。(从 Object 继承。)

TrimExcess()

如果元素数小于当前容量的 90%,将容量设置为 Stack<T> 中的实际元素数。

    注:摘自https://msdn.microsoft.com/zh-cn/library/3278tedw.aspx;

  《5》代码示例:

    

  

 using System;
using System.Collections.Generic; class Example
{
public static void Main()
{
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five"); // A stack can be enumerated without disturbing its contents.
foreach( string number in numbers )
{
Console.WriteLine(number);
} Console.WriteLine("\nPopping '{0}'", numbers.Pop());
Console.WriteLine("Peek at next item to destack: {0}",
numbers.Peek());
Console.WriteLine("Popping '{0}'", numbers.Pop()); // Create a copy of the stack, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Stack<string> stack2 = new Stack<string>(numbers.ToArray()); Console.WriteLine("\nContents of the first copy:");
foreach( string number in stack2 )
{
Console.WriteLine(number);
} // Create an array twice the size of the stack and copy the
// elements of the stack, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * ];
numbers.CopyTo(array2, numbers.Count); // Create a second stack, using the constructor that accepts an
// IEnumerable(Of T).
Stack<string> stack3 = new Stack<string>(array2); Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
foreach( string number in stack3 )
{
Console.WriteLine(number);
} Console.WriteLine("\nstack2.Contains(\"four\") = {0}",
stack2.Contains("four")); Console.WriteLine("\nstack2.Clear()");
stack2.Clear();
Console.WriteLine("\nstack2.Count = {0}", stack2.Count);
}
}

C#中泛型容器Stack<T>的更多相关文章

  1. C#中泛型容器Stack<T>的用法,以及借此实现”撤销/重做”功能

    .Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T ...

  2. (转)Java中的容器详细总结

    Java中的容器详细总结(编辑中) 原文链接:http://anxpp.com/index.php/archives/656/ 注:本文基于 Jdk1.8 编写 通常程序总是根据运行时才知道的某些条件 ...

  3. 泛型容器单元(Generics.Collections)[3]: TStack<T> 堆栈列表

    TQueue 和 TStack, 一个是队列列表, 一个是堆栈列表; 一个是先进先出, 一个是先进后出. TStack 主要有三个方法.一个属性:Push(压栈).Pop(出栈).Peek(查看下一个 ...

  4. java中的容器问题

    小小的总结一下java中的容器问题. 一.三个知识点 1.迭代器 1).java.util.Interator + hasnext(); next(); remove(); 2).java.lang. ...

  5. C#中泛型和单链表

      泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能.泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类 ...

  6. 容器vector的使用总结 容器stack(栈)

    0.头文件:#include<vector>; using namespace std; 1.定义: vector<type> vec; 2.迭代器 vector<typ ...

  7. STL中的容器介绍

    STL中的容器主要包括序列容器.关联容器.无序关联容器等. 一]序列容器 (1) vector vector 是数组的一种类表示,提供自动管理内存的功能,除非其他类型容器有更好满足程序的要求,否则,我 ...

  8. C#中泛型的解释(object,list,var,dynamic的区别)

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能.泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类型的 ...

  9. [C++]STL中的容器

    C++11 STL中的容器 一.顺序容器: vector:可变大小数组: deque:双端队列: list:双向链表: forward_list:单向链表: array:固定大小数组: string: ...

随机推荐

  1. MongoDB Windows 下安装部署

    下面主要是我在Windows上(Win7)安装.运行.安装Windows服务的笔记,以作备忘. 1.下载 下载地址:http://www.mongodb.org/downloads 从其下载页面就可以 ...

  2. [转]directsound抓取麦克风PCM数据封装类

    网上有很多方法从麦克风读取PCM数据,不想一一举例.只是在这里发布一个我自己写的directsound的麦克风PCM数据采集类,通过它,可以很方便的利用directsound技术把麦克风的数据采集到, ...

  3. RSA前台js加密,后台C#解密

    一.需求: 为了安全,项目中前台登陆用的密码需要加密传到后台,后台c#解密登陆密码. 二.解决方案 采用非对称加密算法RSA来达到目的,前台登陆页面一加载便发送一次ajax请求获取后台产生的公钥,用于 ...

  4. Spring JdbcTemplate小结

    提供了JdbcTemplate 来封装数据库jdbc操作细节: 包括: 数据库连接[打开/关闭] ,异常转义 ,SQL执行 ,查询结果的转换 使用模板方式封装 jdbc数据库操作-固定流程的动作,提供 ...

  5. 在VS中安装EF和项目引用EF

    1.通过Visual Studio安装NuGet (1). 打开Visual Studio扩展管理器     (2). 选择联机库,并在搜索中写入NuGet,然后点击搜索结果中NuGet Packag ...

  6. llnq SqlMethods like

    http://www.cnblogs.com/freeliver54/archive/2009/09/05/1560815.html http://www.cnblogs.com/chen1388/a ...

  7. python写的百度贴吧相册下载

    突然想搞个这样的工具,写来写去都不知道在干嘛了,本来两个文件,现在整合在一起了. 乱得不行,懒得整理了,能用就行. 下载部分用了多线程,但是下载一个文件还是用的单线程,也就是没管http头的range ...

  8. some scrum screenshots

    backlog tracking progress Initial Stage Next Stage End

  9. Codeforces Gym 100203E E - bits-Equalizer 贪心

    E - bits-EqualizerTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  10. poj 3613 Cow Relays

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5411   Accepted: 2153 Descri ...