![Page 1: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/1.jpg)
CS313D: ADVANCED
PROGRAMMING LANGUAGE
Lecture 8: Generics Computer Science
department
![Page 2: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/2.jpg)
Lecture Contents
dr.Amal Khalifa, Spr15
Introduction
Why generics?
Generic methods
Overloading generic methods
Generic classes
![Page 3: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/3.jpg)
Overloaded methods are often used to perform similar
operations on different types of data.
Example:
Write a set of overloaded methods to display the elements of an
int array, a double array and a char array, respectively.
Motivation for Generic Methods
dr.Amal Khalifa, Spr15
![Page 4: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/4.jpg)
dr.Amal Khalifa, Spr15
![Page 5: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/5.jpg)
How to use?
dr.Amal Khalifa, Spr15
calls to
DisplayArray method
are identical.
The Compiler
matches the
method based
on its
segnature
![Page 6: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/6.jpg)
Generic Method
dr.Amal Khalifa, Spr15
T Any type
Type checking
Consistent
processing
![Page 7: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/7.jpg)
dr.Amal Khalifa, Spr15
![Page 8: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/8.jpg)
Tip !!
dr.Amal Khalifa, Spr15
![Page 9: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/9.jpg)
We can restrict the types that can be used with a
generic method or class to ensure that they meet
certain requirements—known as a type constraint
C# provides several kinds of type constraints.
A class constraint indicates that the type argument must
be an object of a specific base class or one of its
subclasses.
An interface constraint indicates that the type
argument’s class must implement a specific interface.
Type Constraints
dr.Amal Khalifa, Spr15
![Page 10: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/10.jpg)
IComparable<T> Interface
dr.Amal Khalifa, Spr15
It’s possible to compare two objects of the same type if that
type implements the generic interface IComparable<T>
all IComparable<T> objects are guaranteed to have a
CompareTo(object)
IComparable<T> objects can be used with the sorting and
searching methods of classes in the
System.Collections.Generic
not all types implement interface IComparable<T>
the structures in the Framework Class Library that correspond to
the simple types all implement this interface
![Page 11: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/11.jpg)
Finding Maximum
an interface
constraint:
requires each
of the
method’s
arguments to
be of type
IComparable<T>
![Page 12: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/12.jpg)
dr.Amal Khalifa, Spr15
![Page 13: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/13.jpg)
Overloading Generic Methods
dr.Amal Khalifa, Spr15
A generic method may be overloaded.
Each overloaded method must have a unique signature.
A class can provide two or more generic methods with
the same name but different method parameters.
A generic method can be overloaded by nongeneric
methods with the same method name.
When the compiler encounters a method call, it searches for
the method declaration that best matches the method name
and the argument types specified in the call.
![Page 14: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/14.jpg)
With a generic class, you can use a simple, concise
notation to indicate the actual type(s) that should be
used in place of the class’s type parameter(s).
At compilation time, the compiler ensures your
code’s type safety, and the runtime system replaces
type parameters with type arguments to enable
your client code to interact with the generic class.
Generic Classes
dr.Amal Khalifa, Spr15
![Page 15: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/15.jpg)
Example
dr.Amal Khalifa, Spr15
generic
Stack class,
(e.g., “Stack
of double,”
“Stack of
int,”
“Stack of
char,”
“Stack of
Employee”
![Page 16: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/16.jpg)
dr.Amal Khalifa, Spr15
![Page 17: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/17.jpg)
dr.Amal Khalifa, Spr15
![Page 18: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/18.jpg)
dr.Amal Khalifa, Spr15
![Page 19: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/19.jpg)
Stack app
dr.Amal Khalifa, Spr15
![Page 20: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/20.jpg)
dr.Amal Khalifa, Spr15
![Page 21: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/21.jpg)
dr.Amal Khalifa, Spr15
![Page 22: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/22.jpg)
dr.Amal Khalifa, Spr15
![Page 23: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/23.jpg)
dr.Amal Khalifa, Spr15
![Page 24: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/24.jpg)
dr.Amal Khalifa, Spr15
![Page 25: CS313D: ADVANCED PROGRAMMING LANGUAGE · Computer Science Lecture 8: Generics ... searching methods of classes in the System.Collections.Generic not all types implement interface](https://reader036.vdocument.in/reader036/viewer/2022071216/6048ff1073c0ce76cc2d2d6f/html5/thumbnails/25.jpg)
Chapter 20: 20.1 21.6
That’s all !!
dr.Amal Khalifa, Spr15