cs313d: advanced programming language · computer science lecture 8: generics ... searching methods...

25
CS313D: ADVANCED PROGRAMMING LANGUAGE Lecture 8: Generics Computer Science department

Upload: others

Post on 13-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Chapter 20: 20.1 21.6

That’s all !!

dr.Amal Khalifa, Spr15