Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Code Reusability

Mechanisms

Inheritance

Generics

Base Type

Template
Type

Inheritance

Base Type

Reusable
Code

Car
NumberOfDoors
----------------UnlockDoors()
OpenDoor()
MountOnSeatBelt()

Vehicle
Color
Speed
----------------IngniteEngine()
Drive()

Fields
Methods

Motorcycle
Chain
------------------SitOnIt()
BendToSteer()
DoTheOneWeelTrick()

Generics
A template contains type parameters
<B>

Body

<E>

Engine

<C>

Chassis

<W>

Wheels

Template
SUV

<B>

Reusable
Code

<C>

<E>

<W>
Car

Ferrari

Type
A generic can be
Method()

Generic Type
public class Stack<T>
{
int position;
T[] data = new T[100];
public void Push(T obj){
data[position++] = obj;
}
public T Pop(){
return data[--position];
}
}

int

<T>
Stack<T>

int
Stack<int>

int
public class Stack<T>
{
int position;
T[] data = new T[100];

public class Stack<int>


{
int position;
int[] data = new int[100];

public void Push(T obj){


data[position++] = obj;
}

public void Push(int obj){


data[position++] = obj;
}

public T Pop(){
return data[--position];
}

public int Pop(){


return data[--position];
}

open type

Stack<int> stack = new Stack<int>();


stack.Push(5);
stack.Push(10);
int x = stack.Pop();
int y = stack.Pop();

closed type

No downcast is required
Avoids the possibility of a runtime error
Eliminates the overhead of boxing/unboxing

This generic stack is superior to a nongeneric stack


that uses object in place of T.

Generic Method
static void Swap<T> (ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}

int x = 5, y = 10;
Swap(ref x, ref y);

There is no need to supply type arguments to a generic method,


because the compiler can implicitly infer the type
If there is ambiguity, generic methods can be called with the type
arguments
Swap<int>(ref x, ref y);

Generic Type Parameters


A generic type or method can have multiple parameters
class Dictionary<TKey, TValue> {/*...*/}
var myDic = new Dictionary<int, string>();

Generic Constraints
Can be applied to a type parameter
where T : base-class
where T : interface
where T : class
where T : struct
where T : new()
where U : T

T is or inherits from class


T implements interface
T is a reference type
T is a value type
T has a parameterless constructor
naked type constraint

class List<T>
{
void Add<U>(List<U> items) where U : T {/*...*/}
}

Covariance
Only interface and delegate type parameters can be
specified as variant

public interface IPoppable<out T>


{
T Pop();
}
public class Stack<T> : IPoppable<T>
{
int position;
T[] data = new T[100];
public void Push(T obj) {
data[position++] = obj; }
public T Pop() {
return data[--position]; }

Covariant interface
The out modifier on T
indicates that
T is used only in
output positions
(e.g., return types for methods)

The out modifier flags the interface as covariant

Bear
99

Bear
2
Bear

1
Bear

The out modifier on T indicates that T is used only in output positions (e.g.,
return types for methods)

bears

animals

class Animal { }
class Mouse : Animal { }
class Elephant : Animal { }
Stack<Mouse> mice = new Stack<Mouse>();
Stack<Animal> animals = mice;

Compiler Error
Cannot implicitly convert type Stack<Mouse> to type Stack<Animal>

This prevents us from pushing an Elephant to the stack:


animals.Push(new Elephant());

You might also like