SlideShare a Scribd company logo
Mohammad Shaker 
mohammadshaker.com 
@ZGTRShaker 
2011, 2012, 2013, 2014 
C# Advanced 
L02-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 
Keyword 
Must 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 
Keyword 
Must 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 
Keyword 
Must be static 
Operator 
Arguments 
Why?
Operator Overloading 
classMatrix 
{ 
publicconstintDimSize=3; 
private double[,] m_matrix= new double[DimSize, DimSize]; 
// allow callers to initialize 
public double this[intx, inty] 
{ 
get { return m_matrix[x, y]; } 
set { m_matrix[x, y] = value; } 
} 
// let user add matrices 
public static Matrix operator +(Matrix mat1, Matrix mat2) 
{ 
Matrix newMatrix= new Matrix(); 
for (intx = 0; x < DimSize; x++) 
for(inty = 0; y < DimSize; y++) 
newMatrix[x, y] = mat1[x, y] + mat2[x, y]; 
return newMatrix; 
} 
}
Operator Overloading 
classMatrix 
{ 
publicconstintDimSize=3; 
privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; 
// allow callers to initialize 
publicdoublethis[intx, inty] 
{ 
get{ returnm_matrix[x, y]; } 
set{ m_matrix[x, y] =value; } 
} 
// let user add matrices 
public static Matrix operator +(Matrix mat1, Matrix mat2) 
{ 
Matrix newMatrix= new Matrix(); 
for (intx = 0; x < DimSize; x++) 
for(inty = 0; y < DimSize; y++) 
newMatrix[x, y] = mat1[x, y] + mat2[x, y]; 
return newMatrix; 
} 
}
Operator Overloading 
classMatrix 
{ 
publicconstintDimSize=3; 
privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; 
// allow callers to initialize 
publicdoublethis[intx, inty] 
{ 
get{ returnm_matrix[x, y]; } 
set{ m_matrix[x, y] =value; } 
} 
// let user add matrices 
public static Matrix operator +(Matrix mat1, Matrix mat2) 
{ 
Matrix newMatrix= new Matrix(); 
for (intx = 0; x < DimSize; x++) 
for(inty = 0; y < DimSize; y++) 
newMatrix[x, y] = mat1[x, y] + mat2[x, y]; 
return newMatrix; 
} 
} 
Operator Overloading Declaration
Operator Overloading 
classMatrix 
{ 
publicconstintDimSize=3; 
privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; 
// allow callers to initialize 
publicdoublethis[intx, inty] 
{ 
get{ returnm_matrix[x, y]; } 
set{ m_matrix[x, y] =value; } 
} 
// let user add matrices 
public static Matrix operator +(Matrix mat1, Matrix mat2) 
{ 
Matrix newMatrix= new Matrix(); 
for (intx = 0; x < DimSize; x++) 
for(inty = 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 results 
Matrix mat3 = mat1 + mat2;
Operator Overloading 
classMatrix 
{ 
publicconstintDimSize=3; 
privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; 
// allow callers to initialize 
public double this[intx, inty] 
{ 
get { return m_matrix[x, y]; } 
set { m_matrix[x, y] = value; } 
} 
// let user add matrices 
public static Matrix operator +(Matrix mat1, Matrix mat2) 
{ 
Matrix newMatrix= new Matrix(); 
for (intx = 0; x < DimSize; x++) 
for(inty = 0; y < DimSize; y++) 
newMatrix[x, y] = mat1[x, y] + mat2[x, y]; 
return newMatrix; 
} 
}
Operator Overloading 
classMatrix 
{ 
publicconstintDimSize=3; 
privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; 
// allow callers to initialize 
publicdoublethis[intx, inty] 
{ 
get{ returnm_matrix[x, y]; } 
set{ m_matrix[x, y] =value; } 
} 
// let user add matrices 
public static Matrix operator +(Matrix mat1, Matrix mat2) 
{ 
Matrix newMatrix= new Matrix(); 
for (intx = 0; x < DimSize; x++) 
for(inty = 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 
publicstringthis[intpos] { get{ returnmyData[pos]; } set{ myData[pos] =value; } }
Operator Overloading and Indexers 
classMatrix 
{ 
publicconstintDimSize=3; 
privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; 
// allow callers to initialize 
public double this[intx, inty] 
{ 
get { return m_matrix[x, y]; } 
set { m_matrix[x, y] = value; } 
} 
// let user add matrices 
public static Matrix operator +(Matrix mat1, Matrix mat2) 
{ 
Matrix newMatrix= new Matrix(); 
for (intx = 0; x < DimSize; x++) 
for(inty = 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: intcarWeight= car; 
–explicit 
•which require a cast to be called, saying: intcarWeight= (int)car; 
•All conversions must bestatic, and must either take the type the conversion is defined on, or return that type. 
Car car= new Car(); 
intcarWeight= (int)car;
User-Defined Conversion 
•Let’s have the following user-defined class 
public class MagicNumber 
{ 
public intNumber { get; set; } 
public boolIsMagic{ get; set; } 
}
User-Defined Conversion 
•Let’s have the following user-defined class 
•And let’s say we want to do sthlike this: 
public class MagicNumber 
{ 
public intNumber { get; set; } 
public boolIsMagic{ get; set; } 
} 
intnum= 3; 
MagicNumbermagicNumber= num;
User-Defined Conversion 
•Let’s have the following user-defined class 
•And let’s say we want to do sthlike this: 
•We should then add the following staticmember 
public class MagicNumber 
{ 
public intNumber { get; set; } 
public boolIsMagic{ get; set; } 
} 
intnum= 3; 
MagicNumbermagicNumber= num; 
public static implicit operator MagicNumber(intvalue) 
{ 
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 sthlike this: 
•We should then add the following staticmember 
public class MagicNumber 
{ 
public intNumber { get; set; } 
public boolIsMagic{ get; set; } 
} 
intnum= 3; 
MagicNumbermagicNumber= num; 
public static implicit operator MagicNumber(intvalue) 
{ 
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 sthlike this: 
•We should then add the following staticmember 
public class MagicNumber 
{ 
public intNumber { get; set; } 
public boolIsMagic{ get; set; } 
} 
intnum= 3; 
MagicNumbermagicNumber= num; 
public static implicit operator MagicNumber(intvalue) 
{ 
return new MagicNumber() { Number = value, IsMagic= false }; 
} 
Implicit Because we want to do this 
MagicNumbermagicNumber= num; 
Not explicit like: 
MagicNumbermagicNumber= (MagicNumber)num;
User-Defined Conversion 
•Let’s have the following user-defined class 
•And let’s say we want to do sthlike this: 
•We should then add the following staticmember 
public class MagicNumber 
{ 
public intNumber { get; set; } 
public boolIsMagic{ get; set; } 
} 
intnum= 3; 
MagicNumbermagicNumber= num; 
public static implicit operator MagicNumber(intvalue) 
{ 
return new MagicNumber() { Number = value, IsMagic= false }; 
} 
Means a user-defined conversion from intto MagicNumber(the return type)
Now let’s do this: 
MagicNumbermagicNumber= new MagicNumber() { Number = 3, IsMagic= true }; 
intaNumber= (int)magicNumber;
User-Defined Conversion 
•To do this: 
•We should then add the following staticmember 
MagicNumbermagicNumber= new MagicNumber() { Number = 3, IsMagic= true }; 
intaNumber= (int)magicNumber; 
public static explicitoperator int(MagicNumbermagicNumber) 
{ 
return magicNumber.Number; 
}
Easy right? It’s cool also!

More Related Content

PDF
C# Starter L06-Delegates, Event Handling and Extension Methods
PDF
Pragmatic Functional Refactoring with Java 8
PDF
C# Delegates, Events, Lambda
PPTX
Software design principles SOLID
DOCX
informatics practices practical file
PPTX
Chapter 7 functions (c)
DOCX
JAVA AND MYSQL QUERIES
DOCX
Informatics Practice Practical for 12th class
C# Starter L06-Delegates, Event Handling and Extension Methods
Pragmatic Functional Refactoring with Java 8
C# Delegates, Events, Lambda
Software design principles SOLID
informatics practices practical file
Chapter 7 functions (c)
JAVA AND MYSQL QUERIES
Informatics Practice Practical for 12th class

What's hot (20)

PPTX
Cs1123 8 functions
PPTX
Types of Constructor in C++
PPTX
Flying Futures at the same sky can make the sun rise at midnight
PDF
C++ Course - Lesson 2
PDF
JavaProgrammingManual
PDF
Lec 7 28_aug [compatibility mode]
PDF
Antenna Physical Characetristics
PDF
Polymorphism
PPS
Class method
PPTX
Java script
DOC
Converting Db Schema Into Uml Classes
PDF
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
PDF
Pure Future
PPTX
LinkedIn TBC JavaScript 100: Functions
PPTX
Functional programming principles
PDF
Java_practical_handbook
PPTX
Is java8 a true functional programming language
PPTX
Is java8a truefunctionallanguage
DOCX
Diifeerences In C#
PDF
Static and const members
Cs1123 8 functions
Types of Constructor in C++
Flying Futures at the same sky can make the sun rise at midnight
C++ Course - Lesson 2
JavaProgrammingManual
Lec 7 28_aug [compatibility mode]
Antenna Physical Characetristics
Polymorphism
Class method
Java script
Converting Db Schema Into Uml Classes
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Pure Future
LinkedIn TBC JavaScript 100: Functions
Functional programming principles
Java_practical_handbook
Is java8 a true functional programming language
Is java8a truefunctionallanguage
Diifeerences In C#
Static and const members
Ad

Viewers also liked (7)

PPT
SharePoint 2010 - Architecture and Design aspects
PDF
Microcontroladores pic diseño practico de aplicaciones
PDF
Curso de-mcu-proteus
PDF
Curso de microcontroladores pic18 f4550
PDF
Microcontroladores pic, diseño práctico de aplicaciones 2da parte 16 f87x
PDF
Curso de programacion en c++ para microcontroladores pic 16 f87xx
PDF
Microcontrolador pic16 f84, desarrollo de proyectos ao
SharePoint 2010 - Architecture and Design aspects
Microcontroladores pic diseño practico de aplicaciones
Curso de-mcu-proteus
Curso de microcontroladores pic18 f4550
Microcontroladores pic, diseño práctico de aplicaciones 2da parte 16 f87x
Curso de programacion en c++ para microcontroladores pic 16 f87xx
Microcontrolador pic16 f84, desarrollo de proyectos ao
Ad

Similar to C# Advanced L02-Operator Overloading+Indexers+UD Conversion (20)

PPT
Operator overloading
PPT
Csharp4 operators and_casts
PDF
Unit3_OOP-converted.pdf
PDF
Assign
PPTX
21CSC101T best ppt ever OODP UNIT-2.pptx
PDF
Operator overloading
PPTX
Operators & Casts
PPTX
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
PPTX
OOPS – General Understanding in .NET
PPTX
Polymorphism In c++
PDF
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
PPS
Aae oop xp_08
PPTX
Object Oriented Design and Programming Unit-02
PDF
Ch-4-Operator Overloading.pdf
PPT
C# Tutorial MSM_Murach chapter-04-slides
PPTX
3. Polymorphism.pptx
PDF
PPTX
Polymorphism.Difference between Inheritance & Polymorphism
PPT
Cast and copy constructor Object Oriented Programming
PDF
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
Operator overloading
Csharp4 operators and_casts
Unit3_OOP-converted.pdf
Assign
21CSC101T best ppt ever OODP UNIT-2.pptx
Operator overloading
Operators & Casts
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
OOPS – General Understanding in .NET
Polymorphism In c++
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Aae oop xp_08
Object Oriented Design and Programming Unit-02
Ch-4-Operator Overloading.pdf
C# Tutorial MSM_Murach chapter-04-slides
3. Polymorphism.pptx
Polymorphism.Difference between Inheritance & Polymorphism
Cast and copy constructor Object Oriented Programming
Basics _of_Operator Overloading_Somesh_Kumar_SSTC

More from Mohammad Shaker (20)

PDF
12 Rules You Should to Know as a Syrian Graduate
PDF
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
PDF
Interaction Design L06 - Tricks with Psychology
PDF
Short, Matters, Love - Passioneers Event 2015
PDF
Unity L01 - Game Development
PDF
Android L07 - Touch, Screen and Wearables
PDF
Interaction Design L03 - Color
PDF
Interaction Design L05 - Typography
PDF
Interaction Design L04 - Materialise and Coupling
PDF
Android L05 - Storage
PDF
Android L04 - Notifications and Threading
PDF
Android L09 - Windows Phone and iOS
PDF
Interaction Design L01 - Mobile Constraints
PDF
Interaction Design L02 - Pragnanz and Grids
PDF
Android L10 - Stores and Gaming
PDF
Android L06 - Cloud / Parse
PDF
Android L08 - Google Maps and Utilities
PDF
Android L03 - Styles and Themes
PDF
Android L02 - Activities and Adapters
PDF
Android L01 - Warm Up
12 Rules You Should to Know as a Syrian Graduate
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Interaction Design L06 - Tricks with Psychology
Short, Matters, Love - Passioneers Event 2015
Unity L01 - Game Development
Android L07 - Touch, Screen and Wearables
Interaction Design L03 - Color
Interaction Design L05 - Typography
Interaction Design L04 - Materialise and Coupling
Android L05 - Storage
Android L04 - Notifications and Threading
Android L09 - Windows Phone and iOS
Interaction Design L01 - Mobile Constraints
Interaction Design L02 - Pragnanz and Grids
Android L10 - Stores and Gaming
Android L06 - Cloud / Parse
Android L08 - Google Maps and Utilities
Android L03 - Styles and Themes
Android L02 - Activities and Adapters
Android L01 - Warm Up

Recently uploaded (20)

PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Introduction to Artificial Intelligence
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Nekopoi APK 2025 free lastest update
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Transform Your Business with a Software ERP System
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
history of c programming in notes for students .pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Reimagine Home Health with the Power of Agentic AI​
Introduction to Artificial Intelligence
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Upgrade and Innovation Strategies for SAP ERP Customers
Nekopoi APK 2025 free lastest update
Designing Intelligence for the Shop Floor.pdf
Transform Your Business with a Software ERP System
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Navsoft: AI-Powered Business Solutions & Custom Software Development
Odoo Companies in India – Driving Business Transformation.pdf
history of c programming in notes for students .pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
CHAPTER 2 - PM Management and IT Context
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Softaken Excel to vCard Converter Software.pdf
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx

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

  • 1. Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 C# Advanced L02-Operator Overloading, Indexers & User-Defined Conversion
  • 3. Operator Overloading •Good implementation Car mySedan= new Car(); Garage parkingGarage= new Garage(); mySedan= mySedan+ parkingGarage; // park car in the garage
  • 4. Operator Overloading •How to add two Matrices?
  • 5. Operator Overloading Matrix result = mat1.Add(mat2);
  • 6. Operator Overloading Matrix result = mat1.Add(mat2);
  • 7. Operator Overloading Matrix result = mat1.Add(mat2);
  • 8. Operator Overloading Matrix result = mat1.Add(mat2); Matrix result = Matrix.Add(mat1, mat2);
  • 9. Operator Overloading Matrix result = mat1.Add(mat2); Matrix result = Matrix.Add(mat1, mat2); Matrix result = mat1 + mat2;
  • 10. Matrix result = mat1 + mat2; Operator Overloading Matrix result = mat1.Add(mat2); Matrix result = Matrix.Add(mat1, mat2);
  • 11. Matrix result = mat1 + mat2; Operator Overloading Matrix result = mat1.Add(mat2); Matrix result = Matrix.Add(mat1, mat2);
  • 12. Matrix result = mat1 + mat2; Operator Overloading Matrix result = mat1.Add(mat2); Matrix result = Matrix.Add(mat1, mat2);
  • 13. Operator Overloading •Good implementation Car mySedan= new Car(); Garage parkingGarage= new Garage(); mySedan= mySedan+ parkingGarage; // park car in the garage
  • 14. Operator Overloading •So, how can we implement it? –Dot product public static Matrix operator *(Matrix mat1, Matrix mat2) { // dot product implementation}
  • 15. Operator Overloading •So, how can we implement it? –Dot product public static Matrix operator *(Matrix mat1, Matrix mat2) { // dot product implementation} Return Type Keyword Must be static Operator Arguments
  • 16. Operator Overloading •So, how can we implement it? –Dot product public static Matrix operator *(Matrix mat1, Matrix mat2) { // dot product implementation} Return Type Keyword Must be static Operator Arguments
  • 17. Operator Overloading •So, how can we implement it? –Dot product public static Matrix operator *(Matrix mat1, Matrix mat2) { // dot product implementation} Return Type Keyword Must be static Operator Arguments Why?
  • 18. Operator Overloading classMatrix { publicconstintDimSize=3; private double[,] m_matrix= new double[DimSize, DimSize]; // allow callers to initialize public double this[intx, inty] { get { return m_matrix[x, y]; } set { m_matrix[x, y] = value; } } // let user add matrices public static Matrix operator +(Matrix mat1, Matrix mat2) { Matrix newMatrix= new Matrix(); for (intx = 0; x < DimSize; x++) for(inty = 0; y < DimSize; y++) newMatrix[x, y] = mat1[x, y] + mat2[x, y]; return newMatrix; } }
  • 19. Operator Overloading classMatrix { publicconstintDimSize=3; privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; // allow callers to initialize publicdoublethis[intx, inty] { get{ returnm_matrix[x, y]; } set{ m_matrix[x, y] =value; } } // let user add matrices public static Matrix operator +(Matrix mat1, Matrix mat2) { Matrix newMatrix= new Matrix(); for (intx = 0; x < DimSize; x++) for(inty = 0; y < DimSize; y++) newMatrix[x, y] = mat1[x, y] + mat2[x, y]; return newMatrix; } }
  • 20. Operator Overloading classMatrix { publicconstintDimSize=3; privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; // allow callers to initialize publicdoublethis[intx, inty] { get{ returnm_matrix[x, y]; } set{ m_matrix[x, y] =value; } } // let user add matrices public static Matrix operator +(Matrix mat1, Matrix mat2) { Matrix newMatrix= new Matrix(); for (intx = 0; x < DimSize; x++) for(inty = 0; y < DimSize; y++) newMatrix[x, y] = mat1[x, y] + mat2[x, y]; return newMatrix; } } Operator Overloading Declaration
  • 21. Operator Overloading classMatrix { publicconstintDimSize=3; privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; // allow callers to initialize publicdoublethis[intx, inty] { get{ returnm_matrix[x, y]; } set{ m_matrix[x, y] =value; } } // let user add matrices public static Matrix operator +(Matrix mat1, Matrix mat2) { Matrix newMatrix= new Matrix(); for (intx = 0; x < DimSize; x++) for(inty = 0; y < DimSize; y++) newMatrix[x, y] = mat1[x, y] + mat2[x, y]; return newMatrix; } }
  • 22. Operator Overloading •Performing the operator, just write the following: Matrix mat1 = new Matrix(); Matrix mat2 = new Matrix(); // perform operation and print out results Matrix mat3 = mat1 + mat2;
  • 23. Operator Overloading classMatrix { publicconstintDimSize=3; privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; // allow callers to initialize public double this[intx, inty] { get { return m_matrix[x, y]; } set { m_matrix[x, y] = value; } } // let user add matrices public static Matrix operator +(Matrix mat1, Matrix mat2) { Matrix newMatrix= new Matrix(); for (intx = 0; x < DimSize; x++) for(inty = 0; y < DimSize; y++) newMatrix[x, y] = mat1[x, y] + mat2[x, y]; return newMatrix; } }
  • 24. Operator Overloading classMatrix { publicconstintDimSize=3; privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; // allow callers to initialize publicdoublethis[intx, inty] { get{ returnm_matrix[x, y]; } set{ m_matrix[x, y] =value; } } // let user add matrices public static Matrix operator +(Matrix mat1, Matrix mat2) { Matrix newMatrix= new Matrix(); for (intx = 0; x < DimSize; x++) for(inty = 0; y < DimSize; y++) newMatrix[x, y] = mat1[x, y] + mat2[x, y]; return newMatrix; } }
  • 26. 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 publicstringthis[intpos] { get{ returnmyData[pos]; } set{ myData[pos] =value; } }
  • 27. Operator Overloading and Indexers classMatrix { publicconstintDimSize=3; privatedouble[,] m_matrix=newdouble[DimSize, DimSize]; // allow callers to initialize public double this[intx, inty] { get { return m_matrix[x, y]; } set { m_matrix[x, y] = value; } } // let user add matrices public static Matrix operator +(Matrix mat1, Matrix mat2) { Matrix newMatrix= new Matrix(); for (intx = 0; x < DimSize; x++) for(inty = 0; y < DimSize; y++) newMatrix[x, y] = mat1[x, y] + mat2[x, y]; return newMatrix; } }
  • 31. 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: intcarWeight= car; –explicit •which require a cast to be called, saying: intcarWeight= (int)car; •All conversions must bestatic, and must either take the type the conversion is defined on, or return that type. Car car= new Car(); intcarWeight= (int)car;
  • 32. User-Defined Conversion •Let’s have the following user-defined class public class MagicNumber { public intNumber { get; set; } public boolIsMagic{ get; set; } }
  • 33. User-Defined Conversion •Let’s have the following user-defined class •And let’s say we want to do sthlike this: public class MagicNumber { public intNumber { get; set; } public boolIsMagic{ get; set; } } intnum= 3; MagicNumbermagicNumber= num;
  • 34. User-Defined Conversion •Let’s have the following user-defined class •And let’s say we want to do sthlike this: •We should then add the following staticmember public class MagicNumber { public intNumber { get; set; } public boolIsMagic{ get; set; } } intnum= 3; MagicNumbermagicNumber= num; public static implicit operator MagicNumber(intvalue) { return new MagicNumber() { Number = value, IsMagic= false }; }
  • 35. User-Defined Conversion •Let’s have the following user-defined class •And let’s say we want to do sthlike this: •We should then add the following staticmember public class MagicNumber { public intNumber { get; set; } public boolIsMagic{ get; set; } } intnum= 3; MagicNumbermagicNumber= num; public static implicit operator MagicNumber(intvalue) { return new MagicNumber() { Number = value, IsMagic= false }; } Means a user-defined conversion
  • 36. User-Defined Conversion •Let’s have the following user-defined class •And let’s say we want to do sthlike this: •We should then add the following staticmember public class MagicNumber { public intNumber { get; set; } public boolIsMagic{ get; set; } } intnum= 3; MagicNumbermagicNumber= num; public static implicit operator MagicNumber(intvalue) { return new MagicNumber() { Number = value, IsMagic= false }; } Implicit Because we want to do this MagicNumbermagicNumber= num; Not explicit like: MagicNumbermagicNumber= (MagicNumber)num;
  • 37. User-Defined Conversion •Let’s have the following user-defined class •And let’s say we want to do sthlike this: •We should then add the following staticmember public class MagicNumber { public intNumber { get; set; } public boolIsMagic{ get; set; } } intnum= 3; MagicNumbermagicNumber= num; public static implicit operator MagicNumber(intvalue) { return new MagicNumber() { Number = value, IsMagic= false }; } Means a user-defined conversion from intto MagicNumber(the return type)
  • 38. Now let’s do this: MagicNumbermagicNumber= new MagicNumber() { Number = 3, IsMagic= true }; intaNumber= (int)magicNumber;
  • 39. User-Defined Conversion •To do this: •We should then add the following staticmember MagicNumbermagicNumber= new MagicNumber() { Number = 3, IsMagic= true }; intaNumber= (int)magicNumber; public static explicitoperator int(MagicNumbermagicNumber) { return magicNumber.Number; }
  • 40. Easy right? It’s cool also!