40
Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 C# Advanced L02-Operator Overloading, Indexers & User-Defined Conversion

C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Mohammad Shaker

mohammadshaker.com

@ZGTRShaker

2011, 2012, 2013, 2014

C# AdvancedL02-Operator Overloading, Indexers &

User-Defined Conversion

Page 2: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

Page 3: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

• Good implementation

Car mySedan = new Car(); Garage parkingGarage = new Garage();mySedan = mySedan + parkingGarage; // park car in the garage

Page 4: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

• How to add two Matrices?

Page 5: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

Matrix result = mat1.Add(mat2);

Page 6: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

Matrix result = mat1.Add(mat2);

Page 7: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

Matrix result = mat1.Add(mat2);

Page 8: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

Matrix result = mat1.Add(mat2);

Matrix result = Matrix.Add(mat1, mat2);

Page 9: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

Matrix result = mat1.Add(mat2);

Matrix result = Matrix.Add(mat1, mat2);

Matrix result = mat1 + mat2;

Page 10: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Matrix result = mat1 + mat2;

Operator Overloading

Matrix result = mat1.Add(mat2);

Matrix result = Matrix.Add(mat1, mat2);

Page 11: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Matrix result = mat1 + mat2;

Operator Overloading

Matrix result = mat1.Add(mat2);

Matrix result = Matrix.Add(mat1, mat2);

Page 12: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Matrix result = mat1 + mat2;

Operator Overloading

Matrix result = mat1.Add(mat2);

Matrix result = Matrix.Add(mat1, mat2);

Page 13: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

• Good implementation

Car mySedan = new Car(); Garage parkingGarage = new Garage();mySedan = mySedan + parkingGarage; // park car in the garage

Page 14: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

• So, how can we implement it?

– Dot product

public static Matrix operator *(Matrix mat1, Matrix mat2){

// dot product implementation}

Page 15: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

• So, how can we implement it?

– Dot product

public static Matrix operator *(Matrix mat1, Matrix mat2){

// dot product implementation}

Return Type

KeywordMust be

static

Operator

Arguments

Page 16: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

• So, how can we implement it?

– Dot product

public static Matrix operator *(Matrix mat1, Matrix mat2){

// dot product implementation}

Return Type

KeywordMust be

static

Operator

Arguments

Page 17: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

• So, how can we implement it?

– Dot product

public static Matrix operator *(Matrix mat1, Matrix mat2){

// dot product implementation}

Return Type

KeywordMust be

static

Operator

Arguments

Why?

Page 18: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

class Matrix{

public const int DimSize = 3;private double[,] m_matrix = new double[DimSize, DimSize];

// allow callers to initializepublic double this[int x, int y]{

get { return m_matrix[x, y]; }set { m_matrix[x, y] = value; }

}

// let user add matricespublic static Matrix operator + (Matrix mat1, Matrix mat2){

Matrix newMatrix = new Matrix();

for (int x = 0; x < DimSize; x++)for (int y = 0; y < DimSize; y++)

newMatrix[x, y] = mat1[x, y] + mat2[x, y];

return newMatrix;}

}

Page 19: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

class Matrix{

public const int DimSize = 3;private double[,] m_matrix = new double[DimSize, DimSize];

// allow callers to initializepublic double this[int x, int y]{

get { return m_matrix[x, y]; }set { m_matrix[x, y] = value; }

}

// let user add matricespublic static Matrix operator + (Matrix mat1, Matrix mat2){

Matrix newMatrix = new Matrix();

for (int x = 0; x < DimSize; x++)for (int y = 0; y < DimSize; y++)

newMatrix[x, y] = mat1[x, y] + mat2[x, y];

return newMatrix;}

}

Page 20: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

class Matrix{

public const int DimSize = 3;private double[,] m_matrix = new double[DimSize, DimSize];

// allow callers to initializepublic double this[int x, int y]{

get { return m_matrix[x, y]; }set { m_matrix[x, y] = value; }

}

// let user add matricespublic static Matrix operator + (Matrix mat1, Matrix mat2){

Matrix newMatrix = new Matrix();

for (int x = 0; x < DimSize; x++)for (int y = 0; y < DimSize; y++)

newMatrix[x, y] = mat1[x, y] + mat2[x, y];

return newMatrix;}

}

Operator Overloading Declaration

Page 21: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

class Matrix{

public const int DimSize = 3;private double[,] m_matrix = new double[DimSize, DimSize];

// allow callers to initializepublic double this[int x, int y]{

get { return m_matrix[x, y]; }set { m_matrix[x, y] = value; }

}

// let user add matricespublic static Matrix operator + (Matrix mat1, Matrix mat2){

Matrix newMatrix = new Matrix();

for (int x = 0; x < DimSize; x++)for (int y = 0; y < DimSize; y++)

newMatrix[x, y] = mat1[x, y] + mat2[x, y];

return newMatrix;}

}

Page 22: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

• Performing the operator, just write the following:

Matrix mat1 = new Matrix();Matrix mat2 = new Matrix();

// perform operation and print out resultsMatrix mat3 = mat1 + mat2;

Page 23: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

class Matrix{

public const int DimSize = 3;private double[,] m_matrix = new double[DimSize, DimSize];

// allow callers to initializepublic double this[int x, int y]{

get { return m_matrix[x, y]; }set { m_matrix[x, y] = value; }

}

// let user add matricespublic static Matrix operator + (Matrix mat1, Matrix mat2){

Matrix newMatrix = new Matrix();

for (int x = 0; x < DimSize; x++)for (int y = 0; y < DimSize; y++)

newMatrix[x, y] = mat1[x, y] + mat2[x, y];

return newMatrix;}

}

Page 24: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading

class Matrix{

public const int DimSize = 3;private double[,] m_matrix = new double[DimSize, DimSize];

// allow callers to initializepublic double this[int x, int y]{

get { return m_matrix[x, y]; }set { m_matrix[x, y] = value; }

}

// let user add matricespublic static Matrix operator + (Matrix mat1, Matrix mat2){

Matrix newMatrix = new Matrix();

for (int x = 0; x < DimSize; x++)for (int y = 0; y < DimSize; y++)

newMatrix[x, y] = mat1[x, y] + mat2[x, y];

return newMatrix;}

}

Page 25: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Indexers

Page 26: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Indexers

• Indexers are really easy!

• They allow your class to be used just like an array. On the inside of a class, you

manage a collection of values any way you want.

• They are like properties

public string this[int pos]{

get{

return myData[pos];}set{

myData[pos] = value;}

}

Page 27: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Operator Overloading and Indexers

class Matrix{

public const int DimSize = 3;private double[,] m_matrix = new double[DimSize, DimSize];

// allow callers to initializepublic double this[int x, int y]{

get { return m_matrix[x, y]; }set { m_matrix[x, y] = value; }

}

// let user add matricespublic static Matrix operator + (Matrix mat1, Matrix mat2){

Matrix newMatrix = new Matrix();

for (int x = 0; x < DimSize; x++)for (int y = 0; y < DimSize; y++)

newMatrix[x, y] = mat1[x, y] + mat2[x, y];

return newMatrix;}

}

Page 28: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

User-Defined Conversions

Page 29: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

User-Defined Conversions

Page 30: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

User-Defined ConversionThe Concept

Page 31: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

User-Defined Conversion

• We can’t do this out-of-the-box!

• But in a moment we will. This is a User-defined conversion. Because we are

casting our type (Car) to something else.

• There’re two types of conversion:

– implicit

• which occur automatically when required, saying: int carWeight = car;

– explicit

• which require a cast to be called, saying: int carWeight = (int)car;

• All conversions must be static, and must either take the type the conversion is

defined on, or return that type.

Car car = new Car();int carWeight = (int)car;

Page 32: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

User-Defined Conversion

• Let’s have the following user-defined class

public class MagicNumber{

public int Number { get; set; }public bool IsMagic { get; set; }

}

Page 33: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

User-Defined Conversion

• Let’s have the following user-defined class

• And let’s say we want to do sth like this:

public class MagicNumber{

public int Number { get; set; }public bool IsMagic { get; set; }

}

int num = 3;MagicNumber magicNumber = num;

Page 34: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

User-Defined Conversion

• Let’s have the following user-defined class

• And let’s say we want to do sth like this:

• We should then add the following static member

public class MagicNumber{

public int Number { get; set; }public bool IsMagic { get; set; }

}

int num = 3;MagicNumber magicNumber = num;

public static implicit operator MagicNumber(int value){

return new MagicNumber() { Number = value, IsMagic = false };}

Page 35: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

User-Defined Conversion

• Let’s have the following user-defined class

• And let’s say we want to do sth like this:

• We should then add the following static member

public class MagicNumber{

public int Number { get; set; }public bool IsMagic { get; set; }

}

int num = 3;MagicNumber magicNumber = num;

public static implicit operator MagicNumber(int value){

return new MagicNumber() { Number = value, IsMagic = false };}

Means a user-defined conversion

Page 36: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

User-Defined Conversion

• Let’s have the following user-defined class

• And let’s say we want to do sth like this:

• We should then add the following static member

public class MagicNumber{

public int Number { get; set; }public bool IsMagic { get; set; }

}

int num = 3;MagicNumber magicNumber = num;

public static implicit operator MagicNumber(int value){

return new MagicNumber() { Number = value, IsMagic = false };}

Implicit Because we want to do thisMagicNumber magicNumber = num;

Not explicit like:MagicNumber magicNumber = (MagicNumber)num;

Page 37: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

User-Defined Conversion

• Let’s have the following user-defined class

• And let’s say we want to do sth like this:

• We should then add the following static member

public class MagicNumber{

public int Number { get; set; }public bool IsMagic { get; set; }

}

int num = 3;MagicNumber magicNumber = num;

public static implicit operator MagicNumber(int value){

return new MagicNumber() { Number = value, IsMagic = false };}

Means a user-defined conversion from int to MagicNumber (the return type)

Page 38: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Now let’s do this:MagicNumber magicNumber = new MagicNumber() { Number = 3, IsMagic = true };int aNumber = (int)magicNumber;

Page 39: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

User-Defined Conversion

• To do this:

• We should then add the following static member

MagicNumber magicNumber = new MagicNumber() { Number = 3, IsMagic = true };int aNumber = (int)magicNumber;

public static explicit operator int(MagicNumber magicNumber){

return magicNumber.Number;}

Page 40: C# Advanced L02-Operator Overloading+Indexers+UD Conversion

Easy right?It’s cool also!