Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / MostlySingletonList.cs / 1 / MostlySingletonList.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel { 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 || index != 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("index")); } } 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XamlStackWriter.cs
- AnimationTimeline.cs
- TrimSurroundingWhitespaceAttribute.cs
- httpserverutility.cs
- securitycriticaldataClass.cs
- HtmlProps.cs
- OracleDateTime.cs
- elementinformation.cs
- FlowLayoutPanelDesigner.cs
- FormatterServices.cs
- BulletedList.cs
- Tracer.cs
- GraphicsContainer.cs
- Delay.cs
- SimpleLine.cs
- GenerateTemporaryTargetAssembly.cs
- RadialGradientBrush.cs
- ToolStripItemImageRenderEventArgs.cs
- BitmapEffectGroup.cs
- WindowsAuthenticationModule.cs
- SystemWebCachingSectionGroup.cs
- QueryableFilterRepeater.cs
- TransformedBitmap.cs
- RectangleGeometry.cs
- CompiledQueryCacheEntry.cs
- FontResourceCache.cs
- CodeSnippetStatement.cs
- Literal.cs
- RegexMatchCollection.cs
- SvcMapFileLoader.cs
- TreeView.cs
- DrawingContextDrawingContextWalker.cs
- MimeMultiPart.cs
- SystemIcons.cs
- mansign.cs
- HtmlInputSubmit.cs
- SimpleFieldTemplateUserControl.cs
- CultureInfoConverter.cs
- NodeFunctions.cs
- PackageRelationship.cs
- FontFamilyValueSerializer.cs
- XsdValidatingReader.cs
- PseudoWebRequest.cs
- RowToFieldTransformer.cs
- CompilationUnit.cs
- LocalizedNameDescriptionPair.cs
- PartitionResolver.cs
- Hash.cs
- AvTrace.cs
- TimeSpanValidator.cs
- DbProviderConfigurationHandler.cs
- BindingGroup.cs
- SlipBehavior.cs
- GlyphRunDrawing.cs
- SafeProcessHandle.cs
- EventLogException.cs
- AffineTransform3D.cs
- Roles.cs
- ShaperBuffers.cs
- Column.cs
- XmlCharacterData.cs
- QueryCacheEntry.cs
- ImportCatalogPart.cs
- OperatorExpressions.cs
- DataException.cs
- SpellerStatusTable.cs
- FieldBuilder.cs
- WebPartDisplayModeCollection.cs
- CAGDesigner.cs
- DBConnection.cs
- ScriptingJsonSerializationSection.cs
- SqlDependencyListener.cs
- ArrayEditor.cs
- ListView.cs
- ImageDrawing.cs
- ThreadPool.cs
- ResizeGrip.cs
- XmlSchemaObjectTable.cs
- CharUnicodeInfo.cs
- BamlLocalizationDictionary.cs
- RoutedPropertyChangedEventArgs.cs
- SchemaTypeEmitter.cs
- SystemColors.cs
- _ListenerResponseStream.cs
- TextWriter.cs
- RoleGroupCollectionEditor.cs
- WebEncodingValidatorAttribute.cs
- DiscoveryDocument.cs
- SecurityAttributeGenerationHelper.cs
- SpellCheck.cs
- FocusManager.cs
- DispatcherHooks.cs
- SecondaryViewProvider.cs
- XslTransform.cs
- NodeLabelEditEvent.cs
- Message.cs
- StringPropertyBuilder.cs
- ParserStack.cs
- DesignerActionUI.cs
- OptimalBreakSession.cs