Perator
Operator keywords are used to declare operators in class or structure declaration. The operator declaration can take one of the following four forms:
Public static result-type operator unary-operator (op-type
Operand)
Public static result-type operator binary-operator (op-type
Operand, op-type2 operand2)
Public static implicit operator conv-type-out (conv-type-in
Operand)
Public static explicit operator conv-type-out (conv-type-in
Operand)
Parameters:
The result type of the result-type operator.
Unary-operator: + -! ~ ++-True false
The type of the first (or unique) parameter of op-type.
The name of the first (or unique) parameter in operand.
One of binary-operator: +-*/% & | ^
<>
=! >><>=
<=
Type of the second parameter of the op-type2.
The name of the second parameter of operand2.
The target type of the conv-type-out type conversion operator.
The input type of the conv-type-in type conversion operator.
Note:
The first two forms declare User-Defined operators that reload built-in operators. Not all built-in operators can be overloaded (see overloaded operators ). Op-type and
Op-type2
At least one of them must be a closed type (that is, the type to which the operator belongs, or be understood as a custom type ). For example, this prevents the redefinition of integer addition operators.
The latter two forms declare the conversion operators. Conv-type-in and conv-type-out
There must be a closed type (that is, the conversion operator can only convert from its closed type to another type, or from another type to its closed type ).
The operator can only use value parameters, rather than ref or out parameters.
C #
The comparison operators must be reloaded in pairs. If = is reloaded, it must also be reloaded! =. Otherwise, a compilation error occurs. At the same time, the comparison operator must return a value of the bool type, which is the fundamental difference from other arithmetic operators.
C # The = operator is not allowed to be reloaded. However, if the + operator is reloaded, the compiler automatically uses the + operator to perform the + = Operator operation.
Operator Overloading is actually a function overload. First, call the corresponding operator function through the specified expression, then convert the operator object to the real parameter of the operator function, and then determine the overload of the function to be called based on the type of the real parameter, this process is completed by the compiler.
An optional attribute list (C # programming guide) is available before any operator declaration.
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace OperatorOverLoading
{
Class
Program
{
Static void Main (string [] args)
{
Student s1 = new Student (20, "Tom ");
Student s2 = new Student (18, "Jack ");
Student s3 = s1 + s2;
S3.sayPlus ();
(S1-S2). sayminus ();
Console. readkey ();
}
}
Public class
Student
{
Public Student (){}
Public Student (int age, string name)
{
This. name = name;
This. age = age;
}
Private string name;
Private int age;
Public void sayPlus ()
{
System. Console. WriteLine ("{0} age sum: {1}", this. name, this. age );
}
Public void sayMinus (){
System. Console. WriteLine ("{0} age difference: {1}", this. name,
This. age );
}
// Overwrite the "+" Operator
Public static Student operator + (Student s1, Student s2)
{
Return new Student (s1.age + s2.age, s1.name + "And" +
S2.name );
}
// Overwrite the "-" Operator
Public static student operator-(student S1, student S2 ){
Return new student (math. Abs (s1.age-s2.age), s1.name + "and" +
S2.name );
}
}
}