December 2007
Intermediate to advanced
896 pages
19h 57m
English
You need to understand how the .NET types work for generics and how Generic .NET types differ from regular .NET types.
A couple of quick experiments can show the differences between regular .NET types and generic .NET types. When a regular .NET type is defined, it looks like the FixedSizeCollection type defined in Example 4-1.
Example 4-1. FixedSizeCollection: a regular .NET type
public class FixedSizeCollection { /// <summary> /// Constructor that increments static counter /// and sets the maximum number of items /// and sets the maximum number of items /// and sets the maximum number of items /// </summary> /// <param name="maxItems"></param> public FixedSizeCollection(int maxItems) { FixedSizeCollection.InstanceCount++; this.Items = new object[maxItems]; } /// <summary> /// Add an item to the class whose type /// is unknown as only object can hold any type /// </summary> /// <param name="item">item to add</param> /// <returns>the index of the item added</returns> public int AddItem(object item) { if (this.ItemCount < this.Items.Length) { this.Items[this.ItemCount] = item; return this.ItemCount++; } else throw new Exception("Item queue is full"); } /// <summary> /// Get an item from the class /// </summary> /// <param name="index">the index of the item to get</param> /// <returns>an item of type object</returns> public object GetItem(int index) { if (index >= this.Items.Length && index >= 0) throw new ArgumentOutOfRangeException("index"); ...Read now
Unlock full access