Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / clr / src / BCL / System / Collections / ObjectModel / KeyedCollection.cs / 1 / KeyedCollection.cs
namespace System.Collections.ObjectModel
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
[Serializable()]
[System.Runtime.InteropServices.ComVisible(false)]
[DebuggerTypeProxy(typeof(Mscorlib_KeyedCollectionDebugView<,>))]
[DebuggerDisplay("Count = {Count}")]
public abstract class KeyedCollection: Collection
{
const int defaultThreshold = 0;
IEqualityComparer comparer;
Dictionary dict;
int keyCount;
int threshold;
protected KeyedCollection(): this(null, defaultThreshold) {}
protected KeyedCollection(IEqualityComparer comparer): this(comparer, defaultThreshold) {}
protected KeyedCollection(IEqualityComparer comparer, int dictionaryCreationThreshold) {
if (comparer == null) {
comparer = EqualityComparer.Default;
}
if (dictionaryCreationThreshold == -1) {
dictionaryCreationThreshold = int.MaxValue;
}
if( dictionaryCreationThreshold < -1) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.dictionaryCreationThreshold, ExceptionResource.ArgumentOutOfRange_InvalidThreshold);
}
this.comparer = comparer;
this.threshold = dictionaryCreationThreshold;
}
public IEqualityComparer Comparer {
get {
return comparer;
}
}
public TItem this[TKey key] {
get {
if( key == null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}
if (dict != null) {
return dict[key];
}
foreach (TItem item in Items) {
if (comparer.Equals(GetKeyForItem(item), key)) return item;
}
ThrowHelper.ThrowKeyNotFoundException();
return default(TItem);
}
}
public bool Contains(TKey key) {
if( key == null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}
if (dict != null) {
return dict.ContainsKey(key);
}
if (key != null) {
foreach (TItem item in Items) {
if (comparer.Equals(GetKeyForItem(item), key)) return true;
}
}
return false;
}
private bool ContainsItem(TItem item) {
TKey key;
if( (dict == null) || ((key = GetKeyForItem(item)) == null)) {
return Items.Contains(item);
}
TItem itemInDict;
bool exist = dict.TryGetValue(key, out itemInDict);
if( exist) {
return EqualityComparer.Default.Equals(itemInDict, item);
}
return false;
}
public bool Remove(TKey key) {
if( key == null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}
if (dict != null) {
if (dict.ContainsKey(key)) {
return Remove(dict[key]);
}
return false;
}
if (key != null) {
for (int i = 0; i < Items.Count; i++) {
if (comparer.Equals(GetKeyForItem(Items[i]), key)) {
RemoveItem(i);
return true;
}
}
}
return false;
}
protected IDictionary Dictionary {
get { return dict; }
}
protected void ChangeItemKey(TItem item, TKey newKey) {
// check if the item exists in the collection
if( !ContainsItem(item)) {
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_ItemNotExist);
}
TKey oldKey = GetKeyForItem(item);
if (!comparer.Equals(oldKey, newKey)) {
if (newKey != null) {
AddKey(newKey, item);
}
if (oldKey != null) {
RemoveKey(oldKey);
}
}
}
protected override void ClearItems() {
base.ClearItems();
if (dict != null) {
dict.Clear();
}
keyCount = 0;
}
protected abstract TKey GetKeyForItem(TItem item);
protected override void InsertItem(int index, TItem item) {
TKey key = GetKeyForItem(item);
if (key != null) {
AddKey(key, item);
}
base.InsertItem(index, item);
}
protected override void RemoveItem(int index) {
TKey key = GetKeyForItem(Items[index]);
if (key != null) {
RemoveKey(key);
}
base.RemoveItem(index);
}
protected override void SetItem(int index, TItem item) {
TKey newKey = GetKeyForItem(item);
TKey oldKey = GetKeyForItem(Items[index]);
if (comparer.Equals(oldKey, newKey)) {
if (newKey != null && dict != null) {
dict[newKey] = item;
}
}
else {
if (newKey != null) {
AddKey(newKey, item);
}
if (oldKey != null) {
RemoveKey(oldKey);
}
}
base.SetItem(index, item);
}
private void AddKey(TKey key, TItem item) {
if (dict != null) {
dict.Add(key, item);
}
else if (keyCount == threshold) {
CreateDictionary();
dict.Add(key, item);
}
else {
if (Contains(key)) {
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate);
}
keyCount++;
}
}
private void CreateDictionary() {
dict = new Dictionary(comparer);
foreach (TItem item in Items) {
TKey key = GetKeyForItem(item);
if (key != null) {
dict.Add(key, item);
}
}
}
private void RemoveKey(TKey key) {
BCLDebug.Assert(key != null, "key shouldn't be null!");
if (dict != null) {
dict.Remove(key);
}
else {
keyCount--;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
namespace System.Collections.ObjectModel
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
[Serializable()]
[System.Runtime.InteropServices.ComVisible(false)]
[DebuggerTypeProxy(typeof(Mscorlib_KeyedCollectionDebugView<,>))]
[DebuggerDisplay("Count = {Count}")]
public abstract class KeyedCollection: Collection
{
const int defaultThreshold = 0;
IEqualityComparer comparer;
Dictionary dict;
int keyCount;
int threshold;
protected KeyedCollection(): this(null, defaultThreshold) {}
protected KeyedCollection(IEqualityComparer comparer): this(comparer, defaultThreshold) {}
protected KeyedCollection(IEqualityComparer comparer, int dictionaryCreationThreshold) {
if (comparer == null) {
comparer = EqualityComparer.Default;
}
if (dictionaryCreationThreshold == -1) {
dictionaryCreationThreshold = int.MaxValue;
}
if( dictionaryCreationThreshold < -1) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.dictionaryCreationThreshold, ExceptionResource.ArgumentOutOfRange_InvalidThreshold);
}
this.comparer = comparer;
this.threshold = dictionaryCreationThreshold;
}
public IEqualityComparer Comparer {
get {
return comparer;
}
}
public TItem this[TKey key] {
get {
if( key == null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}
if (dict != null) {
return dict[key];
}
foreach (TItem item in Items) {
if (comparer.Equals(GetKeyForItem(item), key)) return item;
}
ThrowHelper.ThrowKeyNotFoundException();
return default(TItem);
}
}
public bool Contains(TKey key) {
if( key == null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}
if (dict != null) {
return dict.ContainsKey(key);
}
if (key != null) {
foreach (TItem item in Items) {
if (comparer.Equals(GetKeyForItem(item), key)) return true;
}
}
return false;
}
private bool ContainsItem(TItem item) {
TKey key;
if( (dict == null) || ((key = GetKeyForItem(item)) == null)) {
return Items.Contains(item);
}
TItem itemInDict;
bool exist = dict.TryGetValue(key, out itemInDict);
if( exist) {
return EqualityComparer.Default.Equals(itemInDict, item);
}
return false;
}
public bool Remove(TKey key) {
if( key == null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}
if (dict != null) {
if (dict.ContainsKey(key)) {
return Remove(dict[key]);
}
return false;
}
if (key != null) {
for (int i = 0; i < Items.Count; i++) {
if (comparer.Equals(GetKeyForItem(Items[i]), key)) {
RemoveItem(i);
return true;
}
}
}
return false;
}
protected IDictionary Dictionary {
get { return dict; }
}
protected void ChangeItemKey(TItem item, TKey newKey) {
// check if the item exists in the collection
if( !ContainsItem(item)) {
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_ItemNotExist);
}
TKey oldKey = GetKeyForItem(item);
if (!comparer.Equals(oldKey, newKey)) {
if (newKey != null) {
AddKey(newKey, item);
}
if (oldKey != null) {
RemoveKey(oldKey);
}
}
}
protected override void ClearItems() {
base.ClearItems();
if (dict != null) {
dict.Clear();
}
keyCount = 0;
}
protected abstract TKey GetKeyForItem(TItem item);
protected override void InsertItem(int index, TItem item) {
TKey key = GetKeyForItem(item);
if (key != null) {
AddKey(key, item);
}
base.InsertItem(index, item);
}
protected override void RemoveItem(int index) {
TKey key = GetKeyForItem(Items[index]);
if (key != null) {
RemoveKey(key);
}
base.RemoveItem(index);
}
protected override void SetItem(int index, TItem item) {
TKey newKey = GetKeyForItem(item);
TKey oldKey = GetKeyForItem(Items[index]);
if (comparer.Equals(oldKey, newKey)) {
if (newKey != null && dict != null) {
dict[newKey] = item;
}
}
else {
if (newKey != null) {
AddKey(newKey, item);
}
if (oldKey != null) {
RemoveKey(oldKey);
}
}
base.SetItem(index, item);
}
private void AddKey(TKey key, TItem item) {
if (dict != null) {
dict.Add(key, item);
}
else if (keyCount == threshold) {
CreateDictionary();
dict.Add(key, item);
}
else {
if (Contains(key)) {
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate);
}
keyCount++;
}
}
private void CreateDictionary() {
dict = new Dictionary(comparer);
foreach (TItem item in Items) {
TKey key = GetKeyForItem(item);
if (key != null) {
dict.Add(key, item);
}
}
}
private void RemoveKey(TKey key) {
BCLDebug.Assert(key != null, "key shouldn't be null!");
if (dict != null) {
dict.Remove(key);
}
else {
keyCount--;
}
}
}
}
// 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
- FileDetails.cs
- NotImplementedException.cs
- XamlSerializerUtil.cs
- MimeMapping.cs
- XmlNode.cs
- MetabaseServerConfig.cs
- WsdlBuildProvider.cs
- RegistryKey.cs
- ImagingCache.cs
- AccessDataSourceView.cs
- SmtpFailedRecipientsException.cs
- SingleTagSectionHandler.cs
- BufferBuilder.cs
- HttpResponseInternalWrapper.cs
- EnumMemberAttribute.cs
- Menu.cs
- HtmlElementErrorEventArgs.cs
- XmlDigitalSignatureProcessor.cs
- TextTreeUndo.cs
- ADMembershipProvider.cs
- TypedReference.cs
- WindowsSlider.cs
- DataQuery.cs
- SymbolDocumentGenerator.cs
- Errors.cs
- Convert.cs
- Conditional.cs
- DirectoryLocalQuery.cs
- PackageRelationshipCollection.cs
- Label.cs
- PageThemeBuildProvider.cs
- StrokeCollection2.cs
- BitmapPalettes.cs
- SchemaTypeEmitter.cs
- SmtpSpecifiedPickupDirectoryElement.cs
- AddInToken.cs
- AssociationSetEnd.cs
- MaskDesignerDialog.cs
- Model3D.cs
- DependentTransaction.cs
- ParseHttpDate.cs
- MessagingDescriptionAttribute.cs
- PerformanceCounterManager.cs
- TableCell.cs
- RIPEMD160Managed.cs
- ResourceDescriptionAttribute.cs
- GrowingArray.cs
- XmlEntityReference.cs
- IteratorFilter.cs
- ISAPIApplicationHost.cs
- ExpandableObjectConverter.cs
- MethodCallConverter.cs
- StrongNameKeyPair.cs
- BufferedWebEventProvider.cs
- SelectionRangeConverter.cs
- BitmapEffectInputData.cs
- PageCache.cs
- WebReference.cs
- ToolStripItemTextRenderEventArgs.cs
- AnnotationResourceCollection.cs
- ExceptionValidationRule.cs
- ConstraintManager.cs
- CustomError.cs
- FrameworkRichTextComposition.cs
- RowToFieldTransformer.cs
- BorderGapMaskConverter.cs
- Visual3DCollection.cs
- _OSSOCK.cs
- DataExpression.cs
- TreeViewImageIndexConverter.cs
- PriorityQueue.cs
- Path.cs
- ParserStreamGeometryContext.cs
- ToolStripDropDownMenu.cs
- SiteMapNodeCollection.cs
- GridItemProviderWrapper.cs
- OdbcEnvironment.cs
- _SSPISessionCache.cs
- Parser.cs
- XmlILModule.cs
- DesignerHierarchicalDataSourceView.cs
- FlowDocumentView.cs
- Page.cs
- TypeDependencyAttribute.cs
- _LocalDataStore.cs
- KnownAssemblyEntry.cs
- _CommandStream.cs
- Material.cs
- ContainerUIElement3D.cs
- AppDomainUnloadedException.cs
- SmiContextFactory.cs
- SqlTopReducer.cs
- VisualTreeHelper.cs
- WebPartMinimizeVerb.cs
- WindowsFormsHelpers.cs
- AlphaSortedEnumConverter.cs
- PasswordPropertyTextAttribute.cs
- RangeValidator.cs
- DBDataPermissionAttribute.cs
- PixelFormatConverter.cs