Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does C # operator overload mean?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what C# operator overloading means. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Overloading is an important concept in object-oriented, which is an incomplete embodiment of object polymorphism. What people usually call overloading often refers to the overloading of functions. This paper introduces readers to a new overloading model-operator overloading.

Why do you need C # operator overloading

Function overloading provides different parameter methods for the same behavior on an object so that developers can use these different parameters to achieve similar functionality. A set of function overloading decisions generally implement the same function. For example, there are several overloaded versions of the ToString () method on the Object object, which all express the end result of the same behavior, although they accept different parameters. Different parameters cause the signature of the overloaded version of the function to be different, so that the compiler can easily know which overloaded version to call. This technology brings convenience to developers.

Now we try to generalize the definition of overloading. Let's look at the simplest example, where we usually need to declare and initialize a variable of value type like this:

Int digit = 5; string sayHello = "Hello, World"

The "=" operator here is the assignment operator that passes the value on the right to the variable on the left. Here, the type of 5 is int, and the type of "Hello, World" is string, which is exactly the same as the type of variable assigned on the left.

But for the above explanation, we can also think of it this way: the type of 5 is uint or byte, and the type of "Hello, World" is char [], so the types on the left and right of the assignment operation are no longer identical, so how does the compiler handle it? Some people will say that this is "implicit type conversion," which is indeed a good answer, but the rules for implicit type conversion have been determined by the compiler, and if both ends of the assignment operation do not follow the implicit type conversion rules, explicit type conversions are required, for example:

Char c = '2mm; string s = (string) c; int i = (int) c

These explicit type conversions are not suitable for any situation, and people may want to be able to manipulate them with syntax such as assignment, addition, subtraction, multiplication and division in their custom classes.

There may be such a special operational relationship between objects. A typical example is "plural" objects. A complex number is a value type object that contains two fields of type double. Two complex objects can be added, subtracted, multiplied, divided, and equal, but cannot be compared in size. Let's imagine that we can operate on plural classes like this:

Complex C1, c2; C1 = new Complex (3,4); c2 = "4x5i"; var c3 = C1 * c2 /-C1 + c2; if (C1 = = c2) c3 = C1; else c3 = c2

From this code, we can foresee what needs to be achieved by overloading the C # operator:

1. Support the overloading decision of implicit type conversion and explicit type conversion.

2. Support basic binary operators, such as addition, subtraction, multiplication, division, etc.

3. Support basic unary operators, such as negative, inverse, self-increment, self-subtraction, etc.

4. Basic relational operators, such as greater than, less than, equal to and not equal to, are supported.

5. Implement more complex operators, such as ternary operation, [], (), bit operation, etc.

In fact, operator overloading is proposed to solve these problems. Various CLR languages under the CLR framework support operator overloading to varying degrees. Visual Basic also supports operator overloading in version 8.0 (that is, Visual Studio 2005), which has the following features in addition to the advantages listed above.

1. Make the code easier to understand and read.

2. The precedence relation rules of existing operators can be used to deal with the operations between objects.

3. Make the code more flexible and the object more operable.

4. Developers can draw examples because they are familiar with the use of operators on regular value types, so they can introduce these rules directly to custom objects.

Let's go deep into operator overloading in the C # language by introducing the definition of plural classes.

Example of C # operator overloading decision

The following example defines a Complex class that implements four operations of complex addition, subtraction, multiplication and division. The syntax for defining general operators in C # is as follows:

[public | private | protected | internal | internal protected] static | implicit | explicit operator ()

The following is the C # operator overloading (C # 3.0) code.

Struct Complex... {public double Real... {get; set;} public double Imaginary... {get; set;} public Complex (double real, double imaginary): this ()... {this.Real = real; this.Imaginary = imaginary } public static Complex operator + (Complex C1, Complex c2)... {return new Complex... {Real = c1.Real + c2.Real, Imaginary = c1.Imaginary + c2.Imaginary} } public static Complex operator-(Complex C1, Complex c2)... {return new Complex... {Real = c1.Real-c2.Real, Imaginary = c1.Imaginary-c2.Imaginary} } public static Complex operator * (Complex C1, Complex c2) {return new Complex... {Real = c1.Real * c2.Real-c1.Imaginary * c2.Imaginary, Imaginary = c1.Real * c2.Imaginary-c1.Imaginary * c2.Real} } public static Complex operator / (Complex C1, Complex c2) {return new Complex... {Real =-c1.Real * c2.Real + c1.Imaginary * c2.Imaginary, Imaginary =-c1.Real * c2.Imaginary + c1.Imaginary * c2.Real};}}

Because the C # operator overload definition takes effect on the object instance in which it is defined, the operator-and operator / operations can be rewritten to make it easier.

Public static Complex operator-(Complex C1, Complex c2)... {return C1 + new Complex... {Real = c2.Real, Imaginary = c2.Imaginary};} public static Complex operator / (Complex C1, Complex c2). {return C1 * new Complex... {Real =-c2.Real, Imaginary =-c2.Imaginary};}

This makes it easy to use the Complex class:

Var C1 = new Complex (3,4), c2 = new Complex (1,2); var c3 = C1 * c2; Complex c4 = C1-c2 / c3 + C1

To achieve simpler assignments, we also need to implement implicit type conversions (from string to Complex).

Public static implicit operator Complex (string value)... {value = value.TrimEnd ('i'); string [] digits = value.Split ('+','-'); return new Complex... {Real = Convert.ToDouble (digits [0]), Imaginary = Convert.ToDouble (digits [1])};}

You can assign a value to the instance with the following code.

Complex c = "4i 5i"

When the compiler generates these operator overloaded code, it actually defines a method with a special name for each operator that has been overloaded. For example, operator + is actually equivalent to the following code:

[SpecialName] public static Complex op_Addition (Complex C1, Complex c2) {return new Complex... {Real = c1.Real + c2.Real, Imaginary = c1.Imaginary + c2.Imaginary};}

List of C # operator overloads

You can make overloading decisions for the following operators in C #.

◆ +, -,!, ~, +, -, true, false

These unary operators can be overloaded.

◆ +, -, *, /,%, &, |, ^

These binary operators can be overloaded.

◆ = =,! =

These relational operators can be overloaded.

◆ & &, | |

These conditional operators cannot be overloaded, but their values are evaluated by & and |, and these two operators can be overloaded.

◆ []

Array operators cannot be overloaded, but you can define indexers.

◆ ()

Conversion operators cannot be overloaded, but you can define implicit and explicit type conversion operators.

◆ + =,-=, *, / =,% =, & =, | =, ^ =, =

These assignment operators cannot be overloaded, but their values, such as + =, are evaluated by +, while + can be overloaded.

◆ =,.,:,->, new, is, sizeof, typeof

These operators cannot be overloaded.

This is the end of the article on "what is the meaning of C # operator overloading". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report