This topic has not yet been rated - Rate this topic

KeyedByTypeCollection<TItem> Class

Provides a collection whose items are types that serve as keys.

System.Object
  System.Collections.ObjectModel.Collection<TItem>
    System.Collections.ObjectModel.KeyedCollection<Type, TItem>
      System.Collections.Generic.KeyedByTypeCollection<TItem>
        System.ServiceModel.Channels.BindingParameterCollection

Namespace:  System.Collections.Generic
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
public class KeyedByTypeCollection<TItem> : KeyedCollection<Type, TItem>

Type Parameters

TItem

The item types contained in the collection that also serve as the keys for the collection.

The KeyedByTypeCollection<TItem> type exposes the following members.

  Name Description
Public method Supported by Portable Class Library KeyedByTypeCollection<TItem>() Initializes a new instance of the KeyedByTypeCollection<TItem> class.
Public method KeyedByTypeCollection<TItem>(IEnumerable<TItem>) Initializes a new instance of the KeyedByTypeCollection<TItem> class for a specified enumeration of objects.
Top
  Name Description
Public property Supported by Portable Class Library Comparer Gets the generic equality comparer that is used to determine equality of keys in the collection. (Inherited from KeyedCollection<TKey, TItem>.)
Public property Supported by Portable Class Library Count Gets the number of elements actually contained in the Collection<T>. (Inherited from Collection<T>.)
Protected property Supported by Portable Class Library Dictionary Gets the lookup dictionary of the KeyedCollection<TKey, TItem>. (Inherited from KeyedCollection<TKey, TItem>.)
Public property Supported by Portable Class Library Item[TKey] Gets the element with the specified key. (Inherited from KeyedCollection<TKey, TItem>.)
Public property Supported by Portable Class Library Item[Int32] Gets or sets the element at the specified index. (Inherited from Collection<T>.)
Protected property Supported by Portable Class Library Items Gets a IList<T> wrapper around the Collection<T>. (Inherited from Collection<T>.)
Top
  Name Description
Public method Supported by Portable Class Library Add Adds an object to the end of the Collection<T>. (Inherited from Collection<T>.)
Protected method Supported by Portable Class Library ChangeItemKey Changes the key associated with the specified element in the lookup dictionary. (Inherited from KeyedCollection<TKey, TItem>.)
Public method Supported by Portable Class Library Clear Removes all elements from the Collection<T>. (Inherited from Collection<T>.)
Protected method Supported by Portable Class Library ClearItems Removes all elements from the KeyedCollection<TKey, TItem>. (Inherited from KeyedCollection<TKey, TItem>.)
Public method Supported by Portable Class Library Contains(T) Determines whether an element is in the Collection<T>. (Inherited from Collection<T>.)
Public method Supported by Portable Class Library Contains(TKey) Determines whether the collection contains an element with the specified key. (Inherited from KeyedCollection<TKey, TItem>.)
Public method Supported by Portable Class Library CopyTo Copies the entire Collection<T> to a compatible one-dimensional Array, starting at the specified index of the target array. (Inherited from Collection<T>.)
Public method Supported by Portable Class Library Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by Portable Class Library Find<T> Returns the first item in the collection of a specified type.
Public method Supported by Portable Class Library FindAll<T> Returns a collection of objects of type T that are contained in the KeyedByTypeCollection<TItem>.
Public method Supported by Portable Class Library GetEnumerator Returns an enumerator that iterates through the Collection<T>. (Inherited from Collection<T>.)
Public method Supported by Portable Class Library GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Protected method Supported by Portable Class Library GetKeyForItem Gets the type of an item contained in the collection. (Overrides KeyedCollection<TKey, TItem>.GetKeyForItem(TItem).)
Public method Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Supported by Portable Class Library IndexOf Searches for the specified object and returns the zero-based index of the first occurrence within the entire Collection<T>. (Inherited from Collection<T>.)
Public method Supported by Portable Class Library Insert Inserts an element into the Collection<T> at the specified index. (Inherited from Collection<T>.)
Protected method Supported by Portable Class Library InsertItem Inserts an element into the collection at a specific location. (Overrides KeyedCollection<TKey, TItem>.InsertItem(Int32, TItem).)
Protected method Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by Portable Class Library Remove(T) Removes the first occurrence of a specific object from the Collection<T>. (Inherited from Collection<T>.)
Public method Supported by Portable Class Library Remove(TKey) Removes the element with the specified key from the KeyedCollection<TKey, TItem>. (Inherited from KeyedCollection<TKey, TItem>.)
Public method Supported by Portable Class Library Remove<T>() Removes an object of a specified type from the collection.
Public method Supported by Portable Class Library RemoveAll<T> Removes all of the elements of a specified type from the collection.
Public method Supported by Portable Class Library RemoveAt Removes the element at the specified index of the Collection<T>. (Inherited from Collection<T>.)
Protected method Supported by Portable Class Library RemoveItem Removes the element at the specified index of the KeyedCollection<TKey, TItem>. (Inherited from KeyedCollection<TKey, TItem>.)
Protected method Supported by Portable Class Library SetItem Replaces the item at the specified index with a new object. (Overrides KeyedCollection<TKey, TItem>.SetItem(Int32, TItem).)
Public method Supported by Portable Class Library ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Explicit interface implemetation Private method Supported by Portable Class Library ICollection.CopyTo Copies the elements of the ICollection to an Array, starting at a particular Array index. (Inherited from Collection<T>.)
Explicit interface implemetation Private property Supported by Portable Class Library ICollection<T>.IsReadOnly Gets a value indicating whether the ICollection<T> is read-only. (Inherited from Collection<T>.)
Explicit interface implemetation Private property Supported by Portable Class Library ICollection.IsSynchronized Gets a value indicating whether access to the ICollection is synchronized (thread safe). (Inherited from Collection<T>.)
Explicit interface implemetation Private property Supported by Portable Class Library ICollection.SyncRoot Gets an object that can be used to synchronize access to the ICollection. (Inherited from Collection<T>.)
Explicit interface implemetation Private method Supported by Portable Class Library IEnumerable.GetEnumerator Returns an enumerator that iterates through a collection. (Inherited from Collection<T>.)
Explicit interface implemetation Private method Supported by Portable Class Library IList.Add Adds an item to the IList. (Inherited from Collection<T>.)
Explicit interface implemetation Private method Supported by Portable Class Library IList.Contains Determines whether the IList contains a specific value. (Inherited from Collection<T>.)
Explicit interface implemetation Private method Supported by Portable Class Library IList.IndexOf Determines the index of a specific item in the IList. (Inherited from Collection<T>.)
Explicit interface implemetation Private method Supported by Portable Class Library IList.Insert Inserts an item into the IList at the specified index. (Inherited from Collection<T>.)
Explicit interface implemetation Private property Supported by Portable Class Library IList.IsFixedSize Gets a value indicating whether the IList has a fixed size. (Inherited from Collection<T>.)
Explicit interface implemetation Private property Supported by Portable Class Library IList.IsReadOnly Gets a value indicating whether the IList is read-only. (Inherited from Collection<T>.)
Explicit interface implemetation Private property Supported by Portable Class Library IList.Item Gets or sets the element at the specified index. (Inherited from Collection<T>.)
Explicit interface implemetation Private method Supported by Portable Class Library IList.Remove Removes the first occurrence of a specific object from the IList. (Inherited from Collection<T>.)
Top

Only one object of each type is allowed in the collection because the type is the key and each key must be unique. But you can find objects of different types.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
How to use this class
This type's use is not particularly clear. To help, think of this as a map between a type and a value of that type.

You construct a copy of this class using a class or interface common to all the type you will store. That is just a sanity check, and you can use System.Object if you don't want that. You use the Add method to add values just like any other collection. To look-up a value by type you have two options. You can use a System.Type object as an indexer, and you will receive a reference of the common ancestor type. In the alternative, use the Find method constructed with the type you want to look up, and you get back a reference of exactly that type.

An example utilizing Find: (thanks to Pavel Minae)

using System;
using System.Collections.Generic;
static class Program
{
static void Main()
{
var d = new KeyedByTypeCollection<object>();
d.Add(123);
d.Add(123.456);
d.Add('x');
d.Add("foo");
Console.WriteLine(d.Find<int>());
Console.WriteLine(d.Find<double>());
Console.WriteLine(d.Find<char>());
Console.WriteLine(d.Find<string>());
d.Remove<int>();
d.Remove<double>();
d.Remove<string>();
d.Add('y'); // throws
}
}