Exercises


  1. Which of the following can be generic?

    1. classes

    2. methods

    3. properties

    4. operator overloads

    5. structs

    6. enumerations

  2. Extend the Vector class in Ch12Ex01 such that the * operator returns the dot product of two vectors.

    Note

    The dot product of two vectors is defined as the product of their magnitudes multiplied by the cosine of the angle between them.

  3. What is wrong with the following code? Fix it.

    public class Instantiator<T> {    public T instance;        public Instantiator()    {       instance = new T();    } } 
  4. What is wrong with the following code? Fix it.

    public class StringGetter<T> {    public string GetString<T>(T item)    {       return item.ToString();    } }
  5. Create a generic class called ShortCollection<T> that implements IList<T> and consists of a collection of items with a maximum size. This maximum size should be an integer that can be supplied to the constructor of ShortCollection<T>, or defaults to 10. The constructor should also be able to take an initial list of items via a List<T> parameter. The class should function exactly like Collection<T>, but throw an exception of type IndexOutOfRangeException if an attempt is made to add too many items to the collection, or if the List<T> passed to the constructor contains too many items.




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net