[corefx注释说]-System.Collections.Generic.Stack<T>

对C#里边的基础类库有充分的好奇心,所以就心血来潮写一下,这个就不定期更新了,想什么时候写就什么时候写好了。这里弱弱的吐槽一下CSDN的博客。为了以防万一,会在我其他的博客做一下备份。

废话不多说 切入正题:
github上的源代码

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

这里的引用没什么好解释的,Stack仅依赖于System,剩下的两个引用是用来分析测试的。

类的声明


[DebuggerTypeProxy(typeof(StackDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
public class Stack<T> : IEnumerable<T>,
    System.Collections.ICollection,
    IReadOnlyCollection<T>

Attributes没什么好解释的,用在Debug里。
这里声明Stack这样一个泛型类,继承IEnumerable< T >,ICollection, IReadOnlyCollection< T >。三个接口。说明这个类支持foreach循环。是一个只读集合。

字段

private T[] _array;     // Storage for stack elements
private int _size;           // Number of items in the stack.
private int _version;        // Used to keep enumerator in sync w/ collection.
private Object _syncRoot;

private const int DefaultCapacity = 4;

这里是它的字段
包括:栈内元素存储数组,栈内元素个数计数器,和用于同步的一些变量。
并且提供了一个默认的容量大小(4)


        /// <include file='doc\Stack.uex' path='docs/doc[@for="Stack.Stack"]/*' />
        public Stack()
        {
            _array = Array.Empty<T>();
        }

        // Create a stack with a specific initial capacity.  The initial capacity
        // must be a non-negative number.
        /// <include file='doc\Stack.uex' path='docs/doc[@for="Stack.Stack1"]/*' />
        public Stack(int capacity)
        {
            if (capacity < 0)
                throw new ArgumentOutOfRangeException("capacity", SR.ArgumentOutOfRange_NeedNonNegNumRequired);
            _array = new T[capacity];
        }

        // Fills a Stack with the contents of a particular collection.  The items are
        // pushed onto the stack in the same order they are read by the enumerator.
        //
        /// <include file='doc\Stack.uex' path='docs/doc[@for="Stack.Stack2"]/*' />
        public Stack(IEnumerable<T> collection)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");
            _array = EnumerableHelpers.ToArray(collection, out _size);
        }

这里是是它的三个构造方法:
第一种构造方法是直接构造一个空的存储数组,至于其它的字段就不管了。(值类型留给CLR就好辣!~)
第二种构造方法是构造一个具有初始容量的存储数组。并且对容量大小做了相应的判断(必须是自然数)
第三种构造方法是复制一个集合到自己的存储数组。并且对源数据集合进行检查(不得为null)

属性

        /// <include file='doc\Stack.uex' path='docs/doc[@for="Stack.Count"]/*' />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值