Transcript of C# Collection
- 1. Collections
- 2. Collections The .NET Framework provides specialized classes
for data storage and retrieval. These classes provide support for
stacks, queues, lists, and hash tables. Most collection classes
implement the same interfaces, and these interfaces may be
inherited to create new collection classes that fit more
specialized data storage needs.
- 3. Collections A collection is an object that simply allows you
to group other objects. When choosing the way you want to group
your objects, you first have to think about what you want to do
with your objects. The .NET Framework provides specialized classes
for data storage and retrieval. These classes provide support for
Stacks, Queues, Lists, and Hash Tables.
- 4. Collection Classes have the following properties Collection
classes are defined as part of the System.Collections or
System.Collections.Generic namespace. Most collection classes
derive from the interfaces ICollection, IComparer, IEnumerable,
IList, IDictionary, and IDictionaryEnumerator and their generic
equivalents. Using generic collection classes provides increased
type-safety and in some cases can provide better performance,
especially when storing value types.
- 5. There are some pre-defined classes in .Net class library
which have implemented the concept of collection ArrayList Class
HashTable Class Queue Class Stack Class SortedList Class A custom
collection class can be created by implementing ICollection
interface.
- 6. Creating a LIST For dynamic lists, the .NET Framework offers
the generic class List. This class implements the IList,
Icollection, IEnumerable, Ilist,Icollection, and Ienmerable
interfaces. EX: public class Racer:Icomparable,IFormattable {//} We
can create a list for above class using List class Ex: var racers
=new List([params]);
- 7. Creating a List Adding Elements Racers.Add(new
Racer([/*values*/]); Ex: Racer.Add(new
Racer(24,Raghu,Veer,India,54)); With AddRange() method we can add
multiple elements to the collection. Inserting Elements
Recars.Insert(3,new Racer(/*values*/)); Removing elements You can
remove elements from list also. Recers.RemoveAt(3);
- 8. Array Class One of the most basic collection classes. Its
not really a collection class, due to its limitations and its not
even located in the System.Collections namespace, but in the System
namespace. It is strongly typed and can be compared to an array. It
has a fixed size. Arrays can have multiple dimensions. You can
access an item of an array by its index.
- 9. Queue Class Queues are useful for storing messages in the
order they were received for sequential processing. Queues
maintains FIFO (first in first out) system. With the Queue class in
.NET you can create weakly typed collections that are ordered by
the order they are added to the collection. Queue accepts a null
reference as a valid value and allows duplicate elements.
- 10. This class implements a queue as a circular array. Objects
stored in a Queue are inserted at one end and removed from the
other. The capacity of a Queue is the number of elements the Queue
can hold. As elements are added to a Queue, the capacity is
automatically increased as required through reallocation. The
capacity can be decreased by calling TrimToSize.
- 11. Methods in Queue Class Clear GetHashCode Clone GetType
Contains Peek CopyTo ReferenceEquals Dequeue Synchronized Enqueue
ToArray Equals ToString GetEnumerator TrimToSize
- 12. Stack Class Stack is implemented as a circular buffer.
Stack follows LIFO (last in first out ) system. The Stack class is
a weakly typed collection. Push method allows you to add items to
the Stack. Peek method just gets the last object of the Stack. Pop
method also get the last object and then removes that item from the
Stack.
- 13. The capacity of a Stack is the number of elements the Stack
can hold. As elements are added to a Stack, the capacity is
automatically increased as required through reallocation. Stack
accepts a null reference as a valid value and allows duplicate
elements.
- 14. Methods in Stack Clear GetType Clone Peek Contains Pop
CopyTo Push Equals ReferenceEquals GetEnumerator Synchronized
GetHashCode ToArray ToString
- 15. LinkedList LinkedList is a general-purpose linked list. It
supports enumerators and implements the ICollection interface,
consistent with other collection classes in the .NET Framework.
Each node in a LinkedList object is of the type LinkedListNode.
Because the LinkedList is doubly linked, each node points forward
to the Next node and backward to the Previous node. If the
LinkedList is empty, the First and Last properties contain a null
reference
- 16. SortedList A combination of the Array and the Hashtable.
Access items in a SortedList by the index (like the Array), or by
the key (like the Hashtable) Its sorted based on the key object.
The index sequence is based on the sort sequence. A SortedList is
generally slower than the Hashtable, due to the sorting. Weakly
typed.
- 17. SortedList A SortedList object internally maintains two
arrays to store the elements of the list; that is, one array for
the keys and another array for the associated values. Each element
is a key/value pair that can be accessed as a DictionaryEntry
object. A key cannot be a null reference, but a value can be. The
capacity of a SortedList object is the number of elements the
SortedList can hold. As elements are added to a SortedList, the
capacity is automatically increased as required through
reallocation.
- 18. SortedList Class Methods GetKeyList Add GetType Clear
GetValueList Clone IndexOfKey Contains IndexOfValue ContainsKey
ReferenceEquals ContainsValue Remove CopyTo RemoveAt Equals
SetByIndex GetByIndex Synchronized GetEnumerator ToString
GetHashCode TrimToSize GetKey
- 19. HashTable Class Represents a collection of key/value pairs
that are organized based on the hash code of the key. A weakly
typed collection of key-value pairs. Lets you quickly get an object
out of the collection by using its key. Access items in your weakly
typed collection, based on a key, not on an index.
- 20. HashTable Class Each element is a key/value pair stored in
a DictionaryEntry object. A key cannot be a null reference, but a
value can be. Key objects must be immutable as long as they are
used as keys in the Hashtable. When an element is added to the
Hashtable, the element is placed into a bucket based on the hash
code of the key.
- 21. Hashtable Class Methods Add GetEnumerator Clear GetHashCode
Clone GetObjectData Contains GetType ContainsKey OnDeserialization
ContainsValue ReferenceEquals CopyTo Remove Equals Synchronized
ToString
- 22. DICTIONARIES Dictionaries represent a sophisticated data
structure that allows you to access an element based on a key.
Dictionaries are also known as hash tables or maps. .NET framework
offers several dictionary classes. The main class you can use is
Dictionary.
- 23. BIT ARRAYS If you need to deal with a number of bits, you
can use the class BitArray and the struct BitVector32. BitArray is
reference type that contains an array of ints, where for every 32
bits a new integer is used. BitVector32 If you know the number of
bits you need in advance, you can use the BitVector32 structure
instead of BitArray.