我以前都是学出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. hdu4505小Q系列故事——电梯里的爱情

    小Q系列故事——电梯里的爱情 Time Limit: 300/100 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  2. 汇总:Linux下svn命令大全

    svn(subversion)是近年来崛起的版本管理工具,svn服务器有2种运行方式:独立服务器和借助apache.2种方式各有利弊.不管是那种方式,都需要使用各种命令来实现.在本文中,haohtml ...

  3. [前端JS学习笔记]JavaScript prototype 对象

    一.概念介绍 prototype 对象 : 原型对象.在JavaScript中, 每一个对象都继承了另一个对象,后者称为"原型对象". 只有 null 除外,它没有自己的原型对象. ...

  4. SQL SERVER 2008/2012/2012R2/2014 设置开启远程连接(sa配置)

    本文方案适用于Microsoft Sql Server 2008/2012/2012 r2/2014版本,以下简称MSSQLSERVER. MSSQL默认是不允许远程连接,并且禁用sa账户的.如果想要 ...

  5. C#IEnumerator.MoveNext 方法 ()

    将枚举数推进到集合的下一个元素. 命名空间:   System.Collections程序集:  mscorlib(mscorlib.dll 中) 语法: bool MoveNext() 返回值 Ty ...

  6. PC/UVa 题号: 110101/100 The 3n+1 problem (3n+1 问题)

     The 3n + 1 problem  Background Problems in Computer Science are often classified as belonging to a ...

  7. 【M4】非必要不提供default 构造方法

    1.default 构造方法意味着,没有外来信息的情况下,进行初始化,构造出一个对象.对于有些对象是很合理的,比如数值之类的对象,可以初始化为0:对于指针之类的对象,初始化为null:对于集合如vec ...

  8. 从零开始学android开发- layout属性介绍

    android:id 为控件指定相应的ID android:text 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串 android:gravity 指定Vi ...

  9. C#文件读写常用类介绍

    首先要熟悉.NET中处理文件和文件夹的操作.File类和Directory类是其中最主要的两个类.了解它们将对后面功能的实现提供很大的便利.      本节先对和文件系统相关的两个.NET类进行简要介 ...

  10. OpenGL入门学习(转)

    OpenGL入门学习 http://www.cppblog.com/doing5552/archive/2009/01/08/71532.html 说起编程作图,大概还有很多人想起TC的#includ ...