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
- TransactedBatchingElement.cs
- TypeGenericEnumerableViewSchema.cs
- CodeArrayCreateExpression.cs
- SatelliteContractVersionAttribute.cs
- TextElementEnumerator.cs
- coordinatorscratchpad.cs
- SiteMembershipCondition.cs
- DataServiceQueryProvider.cs
- RangeBase.cs
- UserThread.cs
- SortFieldComparer.cs
- GreenMethods.cs
- CellPartitioner.cs
- DeflateStream.cs
- DocumentDesigner.cs
- oledbmetadatacollectionnames.cs
- BadImageFormatException.cs
- CacheOutputQuery.cs
- PropertyInformationCollection.cs
- WebResourceAttribute.cs
- QilSortKey.cs
- ToolStripProgressBar.cs
- HMACRIPEMD160.cs
- Baml2006KeyRecord.cs
- PeerNeighborManager.cs
- SQLString.cs
- StylusDevice.cs
- FilteredAttributeCollection.cs
- Utils.cs
- Sorting.cs
- StickyNoteHelper.cs
- SimpleModelProvider.cs
- ReferentialConstraint.cs
- UpdatePanelControlTrigger.cs
- Run.cs
- IssuanceTokenProviderBase.cs
- ColorConverter.cs
- MenuAutomationPeer.cs
- CounterSampleCalculator.cs
- ProcessThreadCollection.cs
- UnicastIPAddressInformationCollection.cs
- ConfigurationSectionCollection.cs
- ListMarkerSourceInfo.cs
- SQLDoubleStorage.cs
- TreeNodeStyleCollection.cs
- ActiveDocumentEvent.cs
- IPAddressCollection.cs
- KeySplineConverter.cs
- PersonalizableAttribute.cs
- SqlParameterCollection.cs
- BamlResourceDeserializer.cs
- _BasicClient.cs
- DistinctQueryOperator.cs
- PerformanceCounterCategory.cs
- SqlFunctionAttribute.cs
- ReceiveDesigner.xaml.cs
- SendDesigner.xaml.cs
- CqlParser.cs
- XmlNodeWriter.cs
- _ListenerRequestStream.cs
- WebPartMovingEventArgs.cs
- SemanticResultValue.cs
- SiteMapNode.cs
- TextProviderWrapper.cs
- HttpPostProtocolImporter.cs
- ZipIOExtraFieldPaddingElement.cs
- CompilationLock.cs
- activationcontext.cs
- QueryInterceptorAttribute.cs
- RemotingAttributes.cs
- CreateInstanceBinder.cs
- TextTreeTextElementNode.cs
- MasterPageBuildProvider.cs
- GradientBrush.cs
- MobileControl.cs
- SerializationFieldInfo.cs
- XmlSignificantWhitespace.cs
- UnsafeNetInfoNativeMethods.cs
- WebContext.cs
- Task.cs
- TreeNodeSelectionProcessor.cs
- EventData.cs
- BooleanSwitch.cs
- Image.cs
- IndicCharClassifier.cs
- UmAlQuraCalendar.cs
- MemberInfoSerializationHolder.cs
- TreeViewEvent.cs
- Pen.cs
- StorageMappingFragment.cs
- CompilationLock.cs
- View.cs
- FileEnumerator.cs
- HttpDictionary.cs
- CannotUnloadAppDomainException.cs
- FlowchartSizeFeature.cs
- Expression.DebuggerProxy.cs
- Cursor.cs
- QuaternionAnimation.cs
- WindowsGraphics2.cs