Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / IdentityModel / System / IdentityModel / MostlySingletonList.cs / 1305376 / MostlySingletonList.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.IdentityModel { using System.Collections.Generic; // Embed this struct in a class to represent a field of that class // that is logically a list, but contains just one item in all but // the rarest of scenarios. When this class must be passed around // in internal APIs, use it as a ref parameter. struct MostlySingletonListwhere T : class { int count; T singleton; List list; public T this[int index] { get { if (this.list == null) { EnsureValidSingletonIndex(index); return this.singleton; } else { return this.list[index]; } } } public int Count { get { return this.count; } } public void Add(T item) { if (this.list == null) { if (this.count == 0) { this.singleton = item; this.count = 1; return; } this.list = new List (); this.list.Add(this.singleton); this.singleton = null; } this.list.Add(item); this.count++; } static bool Compare(T x, T y) { return x == null ? y == null : x.Equals(y); } public bool Contains(T item) { return IndexOf(item) >= 0; } void EnsureValidSingletonIndex(int index) { if (this.count != 1 ) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeOne))); } if (index != 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("index", SR.GetString(SR.ValueMustBeZero))); } } bool MatchesSingleton(T item) { return this.count == 1 && Compare(this.singleton, item); } public int IndexOf(T item) { if (this.list == null) { return MatchesSingleton(item) ? 0 : -1; } else { return this.list.IndexOf(item); } } public bool Remove(T item) { if (this.list == null) { if (MatchesSingleton(item)) { this.singleton = null; this.count = 0; return true; } else { return false; } } else { bool result = this.list.Remove(item); if (result) { this.count--; } return result; } } public void RemoveAt(int index) { if (this.list == null) { EnsureValidSingletonIndex(index); this.singleton = null; this.count = 0; } else { this.list.RemoveAt(index); this.count--; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.IdentityModel { using System.Collections.Generic; // Embed this struct in a class to represent a field of that class // that is logically a list, but contains just one item in all but // the rarest of scenarios. When this class must be passed around // in internal APIs, use it as a ref parameter. struct MostlySingletonList where T : class { int count; T singleton; List list; public T this[int index] { get { if (this.list == null) { EnsureValidSingletonIndex(index); return this.singleton; } else { return this.list[index]; } } } public int Count { get { return this.count; } } public void Add(T item) { if (this.list == null) { if (this.count == 0) { this.singleton = item; this.count = 1; return; } this.list = new List (); this.list.Add(this.singleton); this.singleton = null; } this.list.Add(item); this.count++; } static bool Compare(T x, T y) { return x == null ? y == null : x.Equals(y); } public bool Contains(T item) { return IndexOf(item) >= 0; } void EnsureValidSingletonIndex(int index) { if (this.count != 1 ) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeOne))); } if (index != 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("index", SR.GetString(SR.ValueMustBeZero))); } } bool MatchesSingleton(T item) { return this.count == 1 && Compare(this.singleton, item); } public int IndexOf(T item) { if (this.list == null) { return MatchesSingleton(item) ? 0 : -1; } else { return this.list.IndexOf(item); } } public bool Remove(T item) { if (this.list == null) { if (MatchesSingleton(item)) { this.singleton = null; this.count = 0; return true; } else { return false; } } else { bool result = this.list.Remove(item); if (result) { this.count--; } return result; } } public void RemoveAt(int index) { if (this.list == null) { EnsureValidSingletonIndex(index); this.singleton = null; this.count = 0; } else { this.list.RemoveAt(index); this.count--; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
![Network programming in C#, Network Programming in VB.NET, Network Programming in .NET](/images/book.jpg)
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- StylusPointPropertyInfoDefaults.cs
- SplitterEvent.cs
- SHA384Managed.cs
- CallbackException.cs
- GuidelineCollection.cs
- CategoriesDocument.cs
- LinqDataSourceContextEventArgs.cs
- ZipIOExtraFieldElement.cs
- SqlReferenceCollection.cs
- SubtreeProcessor.cs
- RadioButtonRenderer.cs
- SapiGrammar.cs
- BindingBase.cs
- DiscreteKeyFrames.cs
- shaperfactoryquerycachekey.cs
- DataGridViewCellPaintingEventArgs.cs
- IConvertible.cs
- HtmlCalendarAdapter.cs
- ObjectAnimationBase.cs
- DispatcherOperation.cs
- XmlDocument.cs
- DocumentsTrace.cs
- DPCustomTypeDescriptor.cs
- ResourceDictionary.cs
- Int32AnimationUsingKeyFrames.cs
- OneOfElement.cs
- HttpModulesSection.cs
- ScalarOps.cs
- TransactionContextManager.cs
- DocumentSchemaValidator.cs
- Touch.cs
- InvalidFilterCriteriaException.cs
- FormViewUpdatedEventArgs.cs
- StylusOverProperty.cs
- LexicalChunk.cs
- TypeUtils.cs
- QueryGeneratorBase.cs
- WindowsAuthenticationEventArgs.cs
- FontCollection.cs
- CodeStatementCollection.cs
- DesignerInterfaces.cs
- URIFormatException.cs
- DynamicValueConverter.cs
- SkinBuilder.cs
- ExpressionNormalizer.cs
- InplaceBitmapMetadataWriter.cs
- WinEventHandler.cs
- RawStylusInputReport.cs
- FilterQuery.cs
- CorrelationExtension.cs
- CircleHotSpot.cs
- BindableTemplateBuilder.cs
- BoundPropertyEntry.cs
- DataException.cs
- RecognizerStateChangedEventArgs.cs
- TypeConverter.cs
- AxisAngleRotation3D.cs
- StylusPlugInCollection.cs
- FileSystemInfo.cs
- DesignTimeHTMLTextWriter.cs
- HebrewNumber.cs
- MsmqHostedTransportConfiguration.cs
- StatusBarDrawItemEvent.cs
- ProjectionPlan.cs
- ServiceParser.cs
- RootBrowserWindowAutomationPeer.cs
- XmlAutoDetectWriter.cs
- PropertyPushdownHelper.cs
- JoinCqlBlock.cs
- BaseValidator.cs
- BasicHttpBindingCollectionElement.cs
- SchemaObjectWriter.cs
- SmtpDigestAuthenticationModule.cs
- ListBox.cs
- BitmapEffectCollection.cs
- PrintPreviewControl.cs
- SocketCache.cs
- CellPartitioner.cs
- InvalidPrinterException.cs
- Collection.cs
- LambdaCompiler.Expressions.cs
- CodeDirectoryCompiler.cs
- BamlTreeNode.cs
- TemplateColumn.cs
- Lease.cs
- DataGridViewButtonColumn.cs
- VersionPair.cs
- TriggerBase.cs
- Matrix3D.cs
- AtlasWeb.Designer.cs
- MultiSelectRootGridEntry.cs
- XsdBuildProvider.cs
- Window.cs
- ToolBarDesigner.cs
- MultipartContentParser.cs
- XmlDataSource.cs
- PaginationProgressEventArgs.cs
- ResourceDefaultValueAttribute.cs
- QilBinary.cs
- UIElement3D.cs