Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CompMod / System / Collections / Generic / Stack.cs / 1305376 / Stack.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*==============================================================================
**
** Class: Stack
**
** Purpose: An array implementation of a generic stack.
**
** Date: January 28, 2003
**
=============================================================================*/
namespace System.Collections.Generic {
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Security.Permissions;
// A simple stack of objects. Internally it is implemented as an array,
// so Push can be O(n). Pop is O(1).
[DebuggerTypeProxy(typeof(System_StackDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
#if !SILVERLIGHT
[Serializable()]
#endif
[System.Runtime.InteropServices.ComVisible(false)]
public class Stack : IEnumerable,
System.Collections.ICollection {
private T[] _array; // Storage for stack elements
private int _size; // Number of items in the stack.
private int _version; // Used to keep enumerator in [....] w/ collection.
#if !SILVERLIGHT
[NonSerialized]
#endif
private Object _syncRoot;
private const int _defaultCapacity = 4;
static T[] _emptyArray = new T[0];
///
public Stack() {
_array = _emptyArray;
_size = 0;
_version = 0;
}
// Create a stack with a specific initial capacity. The initial capacity
// must be a non-negative number.
///
public Stack(int capacity) {
if (capacity < 0)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ArgumentOutOfRange_NeedNonNegNumRequired);
_array = new T[capacity];
_size = 0;
_version = 0;
}
// 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.
//
///
public Stack(IEnumerable collection)
{
if (collection==null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
ICollection c = collection as ICollection;
if( c != null) {
int count = c.Count;
_array = new T[count];
c.CopyTo(_array, 0);
_size = count;
}
else {
_size = 0;
_array = new T[_defaultCapacity];
using(IEnumerator en = collection.GetEnumerator()) {
while(en.MoveNext()) {
Push(en.Current);
}
}
}
}
///
public int Count {
get { return _size; }
}
///
bool System.Collections.ICollection.IsSynchronized {
get { return false; }
}
///
Object System.Collections.ICollection.SyncRoot {
get {
if( _syncRoot == null) {
System.Threading.Interlocked.CompareExchange
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DispatcherProcessingDisabled.cs
- ArrayElementGridEntry.cs
- ChannelServices.cs
- CrossSiteScriptingValidation.cs
- EventArgs.cs
- CharacterHit.cs
- IHttpResponseInternal.cs
- ToolStripItemEventArgs.cs
- TableLayoutStyleCollection.cs
- AddingNewEventArgs.cs
- sqlinternaltransaction.cs
- SecureStringHasher.cs
- MessageUtil.cs
- CellQuery.cs
- SoapTransportImporter.cs
- PeerContact.cs
- latinshape.cs
- webeventbuffer.cs
- PasswordPropertyTextAttribute.cs
- assertwrapper.cs
- PhoneCall.cs
- EntityDataSourceSelectingEventArgs.cs
- DataSourceCache.cs
- InfocardExtendedInformationEntry.cs
- IntegerFacetDescriptionElement.cs
- ButtonBaseAdapter.cs
- CoTaskMemUnicodeSafeHandle.cs
- TreeIterator.cs
- WebException.cs
- OutKeywords.cs
- Substitution.cs
- MD5CryptoServiceProvider.cs
- ListViewItemSelectionChangedEvent.cs
- EdmScalarPropertyAttribute.cs
- ProxyWebPartManager.cs
- OracleException.cs
- RuntimeIdentifierPropertyAttribute.cs
- Point3DValueSerializer.cs
- SymLanguageType.cs
- QuadraticBezierSegment.cs
- SimpleBitVector32.cs
- GenericXmlSecurityToken.cs
- HtmlElement.cs
- WorkflowViewElement.cs
- ListDictionary.cs
- DataGridTextBox.cs
- JoinSymbol.cs
- SafeNativeMethods.cs
- SimpleWebHandlerParser.cs
- ManagedWndProcTracker.cs
- UTF32Encoding.cs
- StreamInfo.cs
- SapiInterop.cs
- HttpListenerContext.cs
- ForwardPositionQuery.cs
- InvalidDataContractException.cs
- GifBitmapDecoder.cs
- ArrayList.cs
- SlipBehavior.cs
- VerificationAttribute.cs
- EntityStoreSchemaGenerator.cs
- SystemKeyConverter.cs
- PauseStoryboard.cs
- StateRuntime.cs
- BuildDependencySet.cs
- CompressStream.cs
- CornerRadius.cs
- TextBox.cs
- ScrollEvent.cs
- BooleanAnimationBase.cs
- BaseDataList.cs
- SafeSystemMetrics.cs
- ExceptionNotification.cs
- baseshape.cs
- TraceSection.cs
- SqlMethodCallConverter.cs
- PersonalizableTypeEntry.cs
- StatementContext.cs
- DataGridViewColumn.cs
- TextServicesHost.cs
- DocumentOrderQuery.cs
- ScrollContentPresenter.cs
- StylusLogic.cs
- ADConnectionHelper.cs
- BrowserTree.cs
- MsmqIntegrationProcessProtocolHandler.cs
- controlskin.cs
- ByteAnimation.cs
- DataTemplate.cs
- HttpCookie.cs
- FrameworkElementFactoryMarkupObject.cs
- COM2Properties.cs
- TextViewBase.cs
- Buffer.cs
- SectionInput.cs
- TypeDescriptor.cs
- HttpClientCredentialType.cs
- XPathDescendantIterator.cs
- ClientOperationFormatterProvider.cs
- PathFigureCollection.cs