Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / IdentityModel / System / IdentityModel / MostlySingletonList.cs / 1 / 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.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- entityreference_tresulttype.cs
- Unit.cs
- WebDescriptionAttribute.cs
- MatrixCamera.cs
- SQLChars.cs
- AssemblyAttributesGoHere.cs
- URLIdentityPermission.cs
- AddInContractAttribute.cs
- DBBindings.cs
- PropertyEmitter.cs
- SafeNativeMethods.cs
- NonVisualControlAttribute.cs
- GPPOINT.cs
- ContextProperty.cs
- RegexStringValidator.cs
- Scripts.cs
- FormViewUpdatedEventArgs.cs
- RestHandlerFactory.cs
- X509Certificate.cs
- ConnectionStringSettings.cs
- DbDataAdapter.cs
- SQLMoney.cs
- ProxyWebPartConnectionCollection.cs
- AutoGeneratedField.cs
- DocumentsTrace.cs
- SharedDp.cs
- GACIdentityPermission.cs
- ObjectDataSourceDisposingEventArgs.cs
- IsolatedStorageFilePermission.cs
- CharacterMetricsDictionary.cs
- DataControlButton.cs
- AstTree.cs
- BinaryObjectInfo.cs
- ScrollProperties.cs
- TextWriter.cs
- StringCollectionEditor.cs
- datacache.cs
- ISFTagAndGuidCache.cs
- XmlWrappingReader.cs
- ApplicationHost.cs
- NativeRecognizer.cs
- RootAction.cs
- CommonProperties.cs
- HtmlGenericControl.cs
- LinqDataSourceView.cs
- MouseEvent.cs
- RightsManagementEncryptionTransform.cs
- SplineKeyFrames.cs
- SSmlParser.cs
- HotSpotCollection.cs
- JournalEntryStack.cs
- SQLBoolean.cs
- DirectionalLight.cs
- QuaternionAnimation.cs
- EventSinkActivity.cs
- UntrustedRecipientException.cs
- ShaderEffect.cs
- TableLayoutPanelCodeDomSerializer.cs
- LinqDataView.cs
- HtmlTableCell.cs
- UInt32.cs
- SettingsProperty.cs
- ResourceLoader.cs
- ButtonPopupAdapter.cs
- XmlSignificantWhitespace.cs
- GroupBoxAutomationPeer.cs
- DomNameTable.cs
- ExchangeUtilities.cs
- SourceSwitch.cs
- DashStyle.cs
- GeneralTransform2DTo3DTo2D.cs
- SchemaSetCompiler.cs
- DocumentPageHost.cs
- TypeUtil.cs
- ContactManager.cs
- MatrixConverter.cs
- HtmlLabelAdapter.cs
- HttpWebRequestElement.cs
- XmlComplianceUtil.cs
- ChangePasswordDesigner.cs
- EmptyQuery.cs
- MeasurementDCInfo.cs
- FrameworkElementFactory.cs
- SchemaImporterExtension.cs
- InvokePattern.cs
- LinkDescriptor.cs
- SerialPinChanges.cs
- SqlBuilder.cs
- DBNull.cs
- CodeDomLoader.cs
- EntitySetDataBindingList.cs
- IPPacketInformation.cs
- LambdaCompiler.Logical.cs
- StrongNameMembershipCondition.cs
- SiteMapHierarchicalDataSourceView.cs
- Rotation3DKeyFrameCollection.cs
- ConnectionManagementElementCollection.cs
- VisualTreeFlattener.cs
- EntityDataSourceWizardForm.cs
- Formatter.cs