Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / HybridCollection.cs / 1305376 / HybridCollection.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Activities { using System; using System.Runtime.Serialization; using System.Collections.Generic; using System.Runtime; using System.Collections.ObjectModel; // used internally for performance in cases where a common usage pattern is a single item [DataContract] class HybridCollectionwhere T : class { [DataMember(EmitDefaultValue = false)] List multipleItems; [DataMember(EmitDefaultValue = false)] T singleItem; public HybridCollection() { } public HybridCollection(T initialItem) { Fx.Assert(initialItem != null, "null is used as a sentinal value and is not a valid item value for a hybrid collection"); this.singleItem = initialItem; } public T this[int index] { get { if (this.singleItem != null) { Fx.Assert(index == 0, "Out of range with a single item"); return this.singleItem; } else if (this.multipleItems != null) { Fx.Assert(index >= 0 && index < this.multipleItems.Count, "Out of range with multiple items."); return this.multipleItems[index]; } Fx.Assert("Out of range. There were no items in the HybridCollection."); return default(T); } } public int Count { get { if (this.singleItem != null) { return 1; } if (this.multipleItems != null) { return this.multipleItems.Count; } return 0; } } protected T SingleItem { get { return this.singleItem; } } protected IList MultipleItems { get { return this.multipleItems; } } public void Add(T item) { Fx.Assert(item != null, "null is used as a sentinal value and is not a valid item value for a hybrid collection"); if (this.multipleItems != null) { this.multipleItems.Add(item); } else if (this.singleItem != null) { this.multipleItems = new List (2); this.multipleItems.Add(this.singleItem); this.multipleItems.Add(item); this.singleItem = null; } else { this.singleItem = item; } } public ReadOnlyCollection AsReadOnly() { if (this.multipleItems != null) { return new ReadOnlyCollection (this.multipleItems); } else if (this.singleItem != null) { return new ReadOnlyCollection (new T[1] { this.singleItem }); } else { return new ReadOnlyCollection (new T[0]); } } // generally used for serialization purposes public void Compress() { if (this.multipleItems != null && this.multipleItems.Count == 1) { this.singleItem = this.multipleItems[0]; this.multipleItems = null; } } public void Remove(T item) { Remove(item, false); } internal void Remove(T item, bool searchingFromEnd) { if (this.singleItem != null) { Fx.Assert(object.Equals(item, this.singleItem), "The given item should be in this list. Something is wrong in our housekeeping."); this.singleItem = null; } else { Fx.Assert(this.multipleItems != null && this.multipleItems.Contains(item), "The given item should be in this list. Something is wrong in our housekeeping."); int position = (searchingFromEnd) ? this.multipleItems.LastIndexOf(item) : this.multipleItems.IndexOf(item); if (position != -1) { this.multipleItems.RemoveAt(position); } } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- VersionValidator.cs
- ZipIOExtraField.cs
- DeclarativeCatalogPartDesigner.cs
- UnsafeNativeMethods.cs
- EventWaitHandleSecurity.cs
- SqlSelectClauseBuilder.cs
- XmlIlGenerator.cs
- FixedDocumentSequencePaginator.cs
- DynamicDiscoSearcher.cs
- MulticastOption.cs
- WebPartManagerInternals.cs
- SymmetricSecurityProtocol.cs
- Thumb.cs
- ModelUIElement3D.cs
- RichTextBoxConstants.cs
- ClientSideQueueItem.cs
- MappingException.cs
- X509CertificateChain.cs
- ParentUndoUnit.cs
- ToolBarButtonDesigner.cs
- Normalization.cs
- DomainConstraint.cs
- ListItemConverter.cs
- URLMembershipCondition.cs
- ContainerVisual.cs
- ExtenderControl.cs
- TextEndOfLine.cs
- localization.cs
- Vector3DAnimationUsingKeyFrames.cs
- EdmItemCollection.cs
- MetadataCache.cs
- ContainerSelectorGlyph.cs
- RowVisual.cs
- ZipFileInfoCollection.cs
- ClientConvert.cs
- TreeWalker.cs
- RuntimeResourceSet.cs
- AddInEnvironment.cs
- ISCIIEncoding.cs
- BufferAllocator.cs
- DataGridViewCellFormattingEventArgs.cs
- RefExpr.cs
- TextTreeExtractElementUndoUnit.cs
- controlskin.cs
- AlphaSortedEnumConverter.cs
- FormViewInsertEventArgs.cs
- StylusShape.cs
- DoWorkEventArgs.cs
- IgnoreSectionHandler.cs
- GenericTextProperties.cs
- WriteableBitmap.cs
- RegexGroup.cs
- PreviewKeyDownEventArgs.cs
- TextTreeUndo.cs
- AssemblyAttributesGoHere.cs
- OverflowException.cs
- FixedTextView.cs
- KeyTime.cs
- VisualProxy.cs
- BamlStream.cs
- DivideByZeroException.cs
- AutomationEvent.cs
- ClientConfigPaths.cs
- FastPropertyAccessor.cs
- XmlSchema.cs
- InstanceOwnerException.cs
- ListItemParagraph.cs
- SimpleMailWebEventProvider.cs
- ConfigurationManagerInternal.cs
- Triplet.cs
- OleDbSchemaGuid.cs
- TerminatorSinks.cs
- SqlDataSourceCommandEventArgs.cs
- SynchronizingStream.cs
- CommandField.cs
- XmlAtomicValue.cs
- BufferBuilder.cs
- WaitHandle.cs
- EdmSchemaAttribute.cs
- MemoryMappedFile.cs
- WindowAutomationPeer.cs
- UInt32.cs
- DataException.cs
- CodeEventReferenceExpression.cs
- GridViewItemAutomationPeer.cs
- ValueQuery.cs
- MenuAdapter.cs
- CompositeControl.cs
- DataTableMapping.cs
- JsonByteArrayDataContract.cs
- WindowsFormsHelpers.cs
- Underline.cs
- StylusPointProperties.cs
- ProviderCommandInfoUtils.cs
- ClosableStream.cs
- TraceLevelHelper.cs
- SequenceDesigner.cs
- ImageDrawing.cs
- DataPagerFieldCollection.cs
- AssemblyName.cs