Sie sind auf Seite 1von 26

Operator Overloading

Operator
Overloading

Operator overloading provides the facility of
specifying the user-defined implementation for
operations wherein one or both operands are of user-
defined class or structure type.


This helps user-defined types to behave much like the
fundamental primitive data types.

Operator
Overloading
+ Operator with Integers
int a = 100;
int b = 200;
int c = a + b;

c = 300;
+ Operator with Strings
string s1 = Hello;
string s2 = World;
string s3 = s1 + s2;

s3 = Hello World
Operators that can be overloaded:-

Unary Operators + - ! ~ ++ -- true false

Binary Operators + - * / % & | ^ << >>

Comparison Operators == != < <= > >=

Operator
Overloading
Operators that can not be
overloaded:-

() [] += -=

*= /= %= &=

!= ^= <<= >>=

new is sizeof typeof
Operator
Overloading

General Syntax:

public static returnType operator op (arg list..)
{
// Own Implementation
}

The operator keyword is used to overload operators.

The operator keyword is used in conjunction with static
methods.

If == operator is overloaded, != operator must be overloaded.

If < operator is overloaded, > operator must also be
overloaded

If <= operator is overloaded, >= operator must also be
overloaded.
Operator Overloading should return a CLS type and not void.

Same Operator can be overloaded with different function
signature

e.g.
public static Matrix operator +(Matrix m1, int[,] m2)
{ }

we can also define one more method like this:
public static Matrix operator + (Matrix m1,Matrix m2)
{ }
Restrictions in Operator Overloading

Number of argument ( Cannot change the no. of operands )

Type of argument ( must contain at least one operand of User-
defined type)

Restriction in method declaration ( must be public and static )

* Overloading BINARY Operator
e.g
public class Point
{
int x,y;

}

// Overloading operator +
public static Point operator +( Point p1, Point p2)
{
return new Point(p1.x+p2.x,p1.y+p2.y);
}

* Overloading UNARY Operator
e.g
public class Point
{
int x,y;

}

// Overloading operator ++
public static Point operator ++( Point p1)
{
return new Point(p1.x+1,p1.y+1);
}
Operator Overloading

* Overloading EQUALITY Operator
e.g
public class Point
{
int x,y;

}

// Overloading operator ==
public static bool operator ==( Point p1,Point p2)
{
return ( (p1.x==p2.x) && (p1.y==p2.y) );
}
Operator Overloading

* Overloading COMPARISON Operator
e.g
public class Point
{
int x,y;

}

// Overloading operator >
public static bool operator >( Point p1,Point p2)
{
return ( (p1.x > p2.x) && (p1.y > p2.y) );
}
Operator Overloading


* Overloading COMPARISON Operator (CONTINUED )


// Overloading operator >=
public static bool operator >= ( Point p1,Point p2)
{
return ( (p1 > p2 ) || (p1==p2) );
}
Operator Overloading
Internal Representation of Overloaded Operators
Operator Overloading
Interacting with Overloaded Operators from
Overloaded Operators

//Operator + via Add()
public static Point Add(Point p1, Point p2)
{
return p1+p2; //Calling the Overloaded Binary Operator +
}

//Using Add() in Main()
Point p1 = new Point(10,20);
Point p1 = new Point(15,25);
Point p3 = Point.Add(p1,p2);
Console.WriteLine(p3);
XML Documentation
XML
Documentation


What is XML ??

Extensible Markup Language (XML) is a markup
language that defines a set of rules for encoding
documents in a format that is both human-readable and
machine-readable.

XML
Documentation



XML
Documentation
e.g.
// This is a single line remark or comment

/*
*This is line 1 of a comment block
*This is line 2 of the comment block
*/

/// <summary>
/// This is a sample summary comment
/// </summary>
XML
Documentation
C# offers several XML tags that can be placed directly within
source files to document the code.

XML Element Meaning

<c> Indicates Short segment of code.
<code> Indicates multiple lines of code.
<example> Mocks up a code example for the described item.
<exception> Documents which exception may be thrown.
<list> Insert a list or table in the documentation file.
<param> Describes a given parameter.
<paramref> Associate a XML tag with specific parameter.
<summary> Documents the executive summary for the
member.

XML
Documentation

XML Element Meaning

<returns> Documents the return value of the member.
<remark> Builds a description for the given member.
<permission> Documents the security constraints of member.



Example:
XML
Documentation
Format Characters
Format characters are placed in the generated .xml file for
the given source code.

Characters Meaning

E Item denotes an event.
F Item represents a field.
M Items denotes a method (constructors & ovrlded
methods).
N Item denotes a namespace.
P Item represents type property.
T Item represents a type (class, enum, struct,
interface..)
XML
Documentation
Transforming XML code comments

With the help of various tools we can transform the XML code
documentation file into HTML based help system.

Several third-party tools are available as free-source.

e.g. Ndoc Application.
Download Link: http://ndoc.sourceforge.net

Any Questions ??


Thank You!!!

Das könnte Ihnen auch gefallen