Operator overloading
The so-called operator overloading is the ability to allow a user to write an expression using a user-defined type .
For example , it is usually necessary to write code similar to the following , Add two numbers into the river , It is clear that sum is two of the number of .
int i=5,j=4;
int sum = I+J;
If you can write an expression of the same type using a user-defined type that represents a subordinate , that is certainly the best thing to do :
Complex i=5;
Complex sum=i+j;
Operator overloading allows operators such as "+" to be overloaded with user-defined types ( that is, to indicate explicit meanings ) . If you do not overload the , the user will need to write the following code :
Complex i= new Complex ();
Complex j= new Complex ();
Complex Sum=complex.add (I,J);
Such code works well , but the Complex type does not work as predefined types in the language .
In my opinion, the so-called operator overloading is to allow struct,class,interface and so on to perform operations .
Implementing C # operator Overloading
Write the keyword public and static, followed by the return type , followed by the operator Keyword , followed by the operator symbol to be declared , and finally the appropriate argument is added to the pair of parentheses .
C # operator overloading Method
1. Writing operator overloading methods
2. After instantiation , perform operator operation
Case :
struct Hour
{
defines a property for easy access to value values
private int value;
public int Value
{
get {return this.value;}
set {this.value = value;}
}
The constructor creates the Hour class according to the int value , and the C # operator Overloads
public Hour (int value)
{
This.value = value;
}
declares a two-dollar operator that adds two Hour classes together
public static Hour operator + (Hour LHS, Hour RHS)
{
return new Hour (Lhs.value + rhs.value);
}
adds a value of type int and a Hour class
public static Hour operator + (Hour lhs, int rhs)
{
Return LHS + new Hour (RHS);
}
public static Hour operator + (int lhs, Hour RHS)
{
return new Hour (LHS) + RHS;
}
}
Analysis : The operator is public , and the search operator must be public .
The operator is static . all operators must be Static of the , The operator is never polymorphic .
And you cannot use virtual,abstract,override or sealed modifiers .
Binary operators(like+)There are two display parameters;there is an explicit argument to the meta-operator.we got aPublic Hour (int value)constructor Function,You can put aintwith theHourAdd,just the first thing to dointconverted toHour
int A;
Hour b=new Hour (b);
Hour sum=new Hour (a);
Although the above code is completely valid , It is not clear or accurate compared to having a Hour and an int directly added .
In order for the Hour to be +int, a two-dollar operator + must be declared, and his first argument is Hour, The second argument is an int.
Write the test code :
Class Program
{
static void Main (string[] args)
{
Hour h = new Hour (10);
Hour sum = h + 10;
Console.WriteLine (sum. Value);
Console.readkey ();
}
}
Case 2:
struct Hour
{
defines a property for easy access to value values
private int value;
public int Value
{
get {return this.value;}
set {this.value = value;}
}
The constructor creates the Hour class according to the int value , and the C # operator Overloads
public Hour (int value)
{
This.value = value;
}
declares a two-dollar operator that adds two Hour classes together
public static Hour operator + (Hour LHS, Hour RHS)
{
return new Hour (Lhs.value + rhs.value);
}
adds a value of type int and a Hour class
public static Hour operator + (Hour lhs, int rhs)
{
Return LHS + new Hour (RHS);
}
public static Hour operator + (int lhs, Hour RHS)
{
return new Hour (LHS) + RHS;
}
defines operator overloading ( unary operators ), and the + + operator has both prefix and suffix forms , C # Can intelligently use the same operator for prefixes and suffixes .
public static Hour operator + + (Hour hvalue)
{
hvalue.value++;
return hvalue;
}
= = is the unary operator and must have two parameters
public static BOOL operator = = (Hour lhs, Hour RHS)
{
return lhs.value = = Rhs.value;
}
public static bool Operator! = (Hour lhs, Hour RHS)
{
return lhs.value! = Rhs.value;
}
}
However, not all operators can be overloaded , and the following operators cannot be overloaded : [] the index operator cannot be overloaded , but the indexer can be defined ;(T) X cannot overload the conversion operator , But you can define a new conversion operator ; = ( assignment operator ) cannot be overloaded .
=,.,?:,??,-;, =>, f (x), as,checked, unchecked, Default,delegate, is, new, sizeof, typeof these operators cannot be overloaded .
Description,comparison Operators(if the overloaded)must be overloaded with pairs;Which means,if the overloaded==,must also be overloaded!=.vice versa,<and the>uiand<=and the>=The same is true.
To overload the operator in the custom class You need to create a method with the correct signature in this class The method must be named operator x, x is the name or symbol of the operator being overloaded The unary operator has a parameter Two operator with two parameters in each case :
public static Complex operator + (Complex C1,complex C2)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C # Advanced Programming 40-day----operator overloading