C # basic syntax Learning (4 ),

Source: Internet
Author: User

C # basic syntax Learning (4 ),

Heavy Load

A method name and a parameter list are called method signatures. C # identify methods based on method signatures. If the two methods have different signatures, they are two different methods.

Overloading can be a method overload (including a constructor overload) or an operator overload. Method overload refers to a group of methods with the same name and different parameter lists. However, different return value types of methods cannot constitute overloading.

1 public static int max(int a, int b)2 {3 4 }5 6 public static int max(int a, int b, int c)7 {8 9 }

In C #, in addition to methods, operators (+,-, and *) can also be overloaded. Operator Overloading allows a class or structure to support certain operator operations. Of course, you can also use methods to achieve the same functions as operator overloading, but in some cases, using Operator Overloading is more in line with People's Daily methods and habits than using methods.

The syntax for Operator Overloading is as follows:

Public static return type operator (parameter list)

{

// Operation Process Code

}

In the definition Syntax of operator overload, public, static, and operator are fixed and must appear in the definition of operator overload. public and static indicate that the method is public and static, operator indicates that this is an operator overload method. The number of parameter lists corresponds to the number of specific operators.

There is also a special type of operator overload, called the type conversion operator overload. Syntax:

Public static explicit | implicit operator conversion destination type (parameter)

The parameter is the data to be converted. The conversion type is the data type after the conversion. The explicit parameter indicates that the conversion must be a display conversion, and the implicit parameter indicates that the conversion can be an implicit conversion.

The following one-dimensional vectors reload "+" and type conversion:

1 namespace OperatorOverload 2 {3 // One-dimensional Vector 4 class Vector 5 {6 private int _ rank = 0; 7 public int rank 8 {9 get {return _ rank ;} 10} 11 12 private double [] _ values; 13 public double [] values 14 {15 get {return _ values;} 16} 17 18 public Vector (int n) 19 {20 _ rank = n; 21 _ values = new double [n]; 22} 23 24 public Vector (double [] doubleArray) 25 {26 _ rank = doubleArray. length; 27 _ values = DoubleArray; 28} 29 30 // + reload 31 public static Vector operator + (Vector a, Vector B) 32 {33 if (. rank = 0 | B. rank = 0) 34 return null; 35 36 // The vector rank is not equal. 37 if (. rank! = B. rank) 38 return null; 39 40 double [] sum = new double [. rank]; 41 double [] valuesa =. values; 42 double [] valuesb = B. values; 43 44 for (int I = 0; I <. rank; I ++) 45 {46 sum [I] = valuesa [I] + valuesb [I]; 47} 48 49 Vector result = new Vector (sum ); 50 51 return result; 52} 53 54 // type conversion overload 55 public static explicit operator double [] (Vector v) 56 {57 return v. values; 58} 59 60 public static explicit operator Vector (double [] array) 61 {62 return new Vector (array ); 63} 64 65 // rewrite ToString Method 66 public override string ToString () 67 {68 string result = "("; 69 for (int I = 0; I <_ rank-1; I ++) 70 {71 result + = _ values [I]. toString () + ","; 72} 73 result + = _ values [_ rank-1] + ")"; 74 75 return result; 76} 77} 78} 79 80 namespace OperatorOverload 81 {82 class Program 83 {84 static void Main (string [] args) 85 {86 Vector a, B, c; 87 88 a = new Vector (new double [] {11.1, 12.1, 13.1, 14.1, 15.1}); 89 B = new Vector (new double [] {22.2, 32.2, 33.2, 34.2, 35.2}); 90 c = a + B; 91 Console. writeLine ("Vector a =" +. toString (); 92 Console. writeLine ("Vector B =" + B. toString (); 93 Console. writeLine ("Vector a + B =" + c. toString (); 94 95 double [] array = new double [] {1, 2, 3, 4, 5}; 96 Vector d = (Vector) array; 97 98 Console. writeLine ("array --> Vector:" + d. toString (); 99 100 Console. readLine (); 101} 102} 103}

Running result

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)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.