Overload
The name of a method and the parameter list of the method are called method signatures. C # Identifies the method based on the method signature, and if the two method signatures are different then they are two different methods.
Overloads can be method overloads (including constructor overloads) and operator overloads. Method overloading refers to a set of methods that have the same name and a different argument list. However, the return value type of the method does not constitute an overload.
1 Public Static intMaxintAintb)2 {3 4 }5 6 Public Static intMaxintAintBintc)7 {8 9}
In C #, except that methods can be overloaded, operators (+ 、-、 *) can also be overloaded. Operator overloading allows a class or struct to support some sort of operator operation. Of course, you can use methods to implement the same functionality as operator overloading, but in some cases, using operator overloading is more consistent with people's everyday ways and habits than using methods.
The operator overloading syntax is as follows:
public static return type operator operator (argument list)
{
Operation Procedure Code
}
In the operator overload definition syntax, public, static, and operator are fixed and must appear in the operator overload definition, and the publicly and static representations of this method are common and quiescent, and operator indicates that this is an operator overloaded method. The number of argument lists corresponds to the specific operator.
There is also a special operator overload called the type conversion operator overload. The syntax is as follows:
public static explicit|implicit operator conversion destination type (parameter)
The parameter is the data to be converted, the conversion destination type is the desired converted data type, explicit indicates that the transformation must be a display transformation, and implicit indicates that the conversion can be an implicit conversion.
The following one-dimensional vectors overload "+" and type conversions:
1 namespaceOperatoroverload2 {3 //one-dimensional vector4 classVector5 {6 Private int_rank =0;7 Public intRank8 {9 Get{return_rank;}Ten } One A Private Double[] _values; - Public Double[] Values - { the Get{return_values;} - } - - PublicVector (intN) + { -_rank =N; +_values =New Double[n]; A } at - PublicVector (Double[] doublearray) - { -_rank =doublearray.length; -_values =Doublearray; - } in - //+ Reload to Public StaticVectoroperator+(vector A, vector b) + { - if(A.rank = =0|| B.rank = =0) the return NULL; * $ //vector rank inequality cannot be addedPanax Notoginseng if(A.rank! =B.rank) - return NULL; the + Double[] sum =New Double[A.rank]; A Double[] Valuesa =a.values; the Double[] Valuesb =b.values; + - for(inti =0; i < A.rank; i++) $ { $Sum[i] = Valuesa[i] +Valuesb[i]; - } - theVector result =NewVector (sum); - Wuyi returnresult; the } - Wu //type conversion Overloads - Public Static Explicit operator Double[] (Vector v) About { $ returnv.values; - } - - Public Static Explicit operatorVector (Double[] array) A { + return NewVector (array); the } - $ //overriding the ToString method the Public Override stringToString () the { the stringresult ="("; the for(inti =0; I < _rank-1; i++) - { inResult + = _values[i]. ToString () +", "; the } theResult + = _values[_rank-1] +")"; About the returnresult; the } the } + } - the namespaceOperatoroverloadBayi { the class Program the { - Static voidMain (string[] args) - { the Vector A, B, C; the theA =NewVector (New Double[] {11.1,12.1,13.1,14.1,15.1 }); theb =NewVector (New Double[] {22.2,32.2,33.2,34.2,35.2}); -c = A +b; theConsole.WriteLine ("Vector a="+a.tostring ()); theConsole.WriteLine ("Vector b="+b.tostring ()); theConsole.WriteLine ("Vector a+b="+c.tostring ());94 the Double[] Array =New Double[] {1,2,3,4,5 }; theVector d =(Vector) array; the 98Console.WriteLine ("Array-->vector:"+d.tostring ()); About - console.readline ();101 }102 }103}
Run results
Vector a= (11.1,12.1,13.1,14.1,15.1) Vector b=(22.2,32.2,33.2,34.2,35.2) Vector a+b= (33.3,44.3,46.3,48.3,50.3) Array-->vector: (1,2,3,4,5)
C # Basic Grammar Learning (iv)