Upload
mohammad-shaker
View
196
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
Mohammad Shaker
mohammadshaker.com
@ZGTRShaker
2011, 2012, 2013, 2014
C# AdvancedL02-Operator Overloading, Indexers &
User-Defined Conversion
Operator Overloading
Operator Overloading
• Good implementation
Car mySedan = new Car(); Garage parkingGarage = new Garage();mySedan = mySedan + parkingGarage; // park car in the garage
Operator Overloading
• How to add two Matrices?
Operator Overloading
Matrix result = mat1.Add(mat2);
Operator Overloading
Matrix result = mat1.Add(mat2);
Operator Overloading
Matrix result = mat1.Add(mat2);
Operator Overloading
Matrix result = mat1.Add(mat2);
Matrix result = Matrix.Add(mat1, mat2);
Operator Overloading
Matrix result = mat1.Add(mat2);
Matrix result = Matrix.Add(mat1, mat2);
Matrix result = mat1 + mat2;
Matrix result = mat1 + mat2;
Operator Overloading
Matrix result = mat1.Add(mat2);
Matrix result = Matrix.Add(mat1, mat2);
Matrix result = mat1 + mat2;
Operator Overloading
Matrix result = mat1.Add(mat2);
Matrix result = Matrix.Add(mat1, mat2);
Matrix result = mat1 + mat2;
Operator Overloading
Matrix result = mat1.Add(mat2);
Matrix result = Matrix.Add(mat1, mat2);
Operator Overloading
• Good implementation
Car mySedan = new Car(); Garage parkingGarage = new Garage();mySedan = mySedan + parkingGarage; // park car in the garage
Operator Overloading
• So, how can we implement it?
– Dot product
public static Matrix operator *(Matrix mat1, Matrix mat2){
// dot product implementation}
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
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
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?
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
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
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
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
• Performing the operator, just write the following:
Matrix mat1 = new Matrix();Matrix mat2 = new Matrix();
// perform operation and print out resultsMatrix mat3 = mat1 + mat2;
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
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;}
}
Indexers
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;}
}
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;}
}
User-Defined Conversions
User-Defined Conversions
User-Defined ConversionThe Concept
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;
User-Defined Conversion
• Let’s have the following user-defined class
public class MagicNumber{
public int Number { get; set; }public bool IsMagic { get; set; }
}
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;
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 };}
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
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;
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)
Now let’s do this:MagicNumber magicNumber = new MagicNumber() { Number = 3, IsMagic = true };int aNumber = (int)magicNumber;
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;}
Easy right?It’s cool also!