Learn the C # advanced programming 3--operator overloading

Source: Internet
Author: User

The overload of the operator. C + + developers should be familiar with this concept, but this is really new for Java and VB developers.

For some numeric operations, it can be cumbersome to specify an operation rule by means of a method, and the overloading of the operator is available.

Cases:

Matrix A,b,c; Defining a Matrix Object

Marix d=c* (A+B);

If you use a language that does not support operator overloading, you must define a method that is evaluated by calling the method:

Marix d=c.muliply (A.add (b));

The results are not intuitive.

  

Overloading of operators, which are often used in mathematical or physical modeling (such as coordinates, vectors, matrices, function operations, etc.). There is a shape, financial aspect. Of course, if you're going to calculate a date, like two datetime multiplication, no one will stop you, even if it doesn't make sense, haha.

In fact, we used the overloads of the operators before we wrote the code, although we didn't define overloaded operators ourselves, but C # has helped us by default.

int myint=3; UINT myuint=2; double mydouble=13.2; long mylong=myint+myuint; double mydouble=mydouble+myInt; double mydouble2=myint+mydouble;

The "+" operator, which takes two parameters and then finds the overloaded method of the most matching operator based on the argument

The long mylong=myint+myuint above, and double mydouble2=myint+mydouble, are called by different versions of the overloads.

In C #, the compiler automatically matches the most appropriate overloaded method, as with double and int data, the "+" operator does not have an overload with this compound parameter, so the compiler thinks that the most matching "+" overload is two double and the resulting number is a double type.

So what happens to custom types, then you have to define the overloads of the operators yourself.

Defines a vector structure that represents a three-dimensional mathematical coordinate (x, y, z)

structVector {Private Doublex, y, Z;  PublicVector (DoubleXDoubleYDoublez) { This. x =x;  This. y =y;  This. z =Z; }         Publicvector (vector vec) { This. x =vec.x;  This. y =vec.y;  This. z =vec.z; }                 Public Override stringToString () {return "("+ x +","+ y +","+ z +")"; }        //+ operator Overloading         Public StaticVectoroperator+(Vector lhs,vector rhs) {vector result=NewVector (); Result.x=lhs.x+rhs.x; Result.y=lhs.y+Rhs.y; Result.z=lhs.z+rhs.z; returnResult//returns the value after the addition of two 3-D coordinates        }    }
  

static void Main (string[] args)
{
First, define a 3-D coordinate object
Vector vec1 = new vector (1, 0,-2);
Vector vec2 = new vector (2,-1, 5);
Vector VEC3 = vec1 + vec2;

Console.WriteLine (VEC1. ToString ()); Output (1,0,-2)
Console.WriteLine (VEC2. ToString ()); Output (2,-1,5)
Console.WriteLine (VEC3. ToString ()); Output (3,-1,3)
Console.ReadLine ();
}

Of course, you can also define more overloaded operations, add, subtract, and multiply. You can also multiply scalars with vectors, such as

public static Vector operator * (double lhs,vector RHS)
{
return new Vector (LHS * rhs.x, LHS * rhs.y, LHS * rhs.z);
}

This overload is called for 2.0* (All-in-one) and for (a). However, for (three-way), you have to overload a method

public static vector operator * (Vector lhs,double RHS)
{
return RHS * LHS;
}

Here, there is no need to re-operate the process as in the previous method, reusing the above code directly. This illustrates the simple idea of code and also improves the maintainability of the code.

Believe, next write vector multiplication words, also is not difficult, not much write.

Of course, there are the overloads of the comparator, including the following 3 pairs:

= = and! =

> and <

>= and <=

In fact, the operation of overloaded methods is the same, the above example focus "+" operator is changed to the comparison operator.

public static BOOL operator = = (Vector lhs,vector RHS)
{
Return???
}

Actually wrote for one hours, 0:11, hurriedly sleep!

Next: What is the conversion of a user-defined type to a common system prefab numeric type conversion (such as float to double)?

Next period narrative, a user-defined type of casting

Reference page:

Http://www.yuanjiaocheng.net/CSharp/Csharp-fileinfo.html

Http://www.yuanjiaocheng.net/mvc/mvc-models.html

Http://www.yuanjiaocheng.net/entity/delete-entity.html

Http://www.yuanjiaocheng.net/Jsp/first.html

Http://www.yuanjiaocheng.net/ASPNET-CORE/attribute-route.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-while-loop.html

Http://www.yuanjiaocheng.net/CSharp/csharp-Predicate-delegate.html

Http://www.yuanjiaocheng.net/ASPNET-CORE/core-configuration.html

Http://www.yuanjiaocheng.net/Linq/linq-lambda-expression.html

Http://www.yuanjiaocheng.net/Java/internal-details-of-hello-java-program.html

Http://www.yuanjiaocheng.net/ASPNET-CORE/core-identity-migrations.html

Learn the C # advanced programming 3--operator overloading

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.