In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to realize the overloading of C # operator". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought and go deep into it slowly. Let's study and learn how to achieve C# operator overloading.
What is a C # operator overload?
Is the ability to allow users to write expressions using user-defined types.
For example, you usually need to write code similar to the following to add two numbers. Obviously, sum is the sum of two numbers.
Int I = 5
Int sum = I + j
If you can write expressions of the same type using user-defined types that represent the plural, of course it is * but:
Complex I = 5
Complex sum = I + j
Operator overloading allows you to overload (that is, specify an explicit meaning) operators such as "+" for user-defined types. If not overloaded, the user needs to write the following code:
Complex I = new Complex (5)
Complex sum = Complex.Add (I, j)
This code works fine, but Complex types don't work as well as predefined types in the language.
In my opinion, operator overloading allows struct, class, Interface, and so on to perform operations.
Implement the overloading of the C # operator if necessary.
First write the keywords public and static, followed by the return type, followed by the operator keyword, followed by the operator symbol to be declared, and * add the appropriate parameters in a pair of parentheses.
Such as public static Hour operator+ (Hour lhs,Hour rhs) {.} in struct Hour in the following example
C # operator overload method:
1. Write operator overloading methods.
2. After instantiation, the operator operation is performed
The following is illustrated with the C # operator overload code:
Class Program {static void Main (string [] args) {Hour hrValue1 = new Hour (10); Hour hrValue2 = new Hour (20); / 2. Get two Hour addition results Hour hrSum = hrValue1 + hrValue2; / / get Hour+Int results Hour hrSumInt = hrValue1 + 10; / / get Int+Hour results Hour hrIntSum = 11 + hrValue2; Console.WriteLine ("hrValue1 + hrValue2 = {0}", hrSum.iValue) Console.WriteLine ("hrValue1 + 10 = {0}", hrSumInt.iValue); Console.WriteLine ("11 + hrValue2 = {0}", hrIntSum.iValue); Console.ReadKey ();} struct Hour {private int value; / / this constructor creates Hour classes based on int values, and the C # operator overloads public Hour (int iValue) {this.value = iValue;} / / to define an attribute to facilitate access to the value public int iValue {get {return value The}} / / C # operator overloads / / 1, declares a binary operator, and adds two Hour classes together public static Hour operator+ (Hour lhs,Hour rhs) {return new Hour (lhs.value + rhs.value);} / * operator is public. All operators must be public. Operators must be static. All operators must be static, operations are never polymorphic, and virtual, abstract, override, or sealed modifiers cannot be used. The binary operator (such as +) has two explicit parameters; the unary operator has an explicit parameter we have the public Hour (int iValue) constructor, we can add an int to Hour, but first convert int to Hour hour a =; int b =; Hour sum=a+new Hour (b); although the above code is completely valid, it is neither clear nor accurate than having a Hour and an int add directly. In order for Hour to be + int, you must declare a binary operator + whose * argument is Hour and the second argument is an int. The C # operator overloads * / public static Hour operator+ (Hour lhs,int rhs) {return lhs + new Hour (rhs);} / to make an int + hour. Public static Hour operator + (int lhs, Hour rhs) {return new Hour (lhs) + rhs;}
Example 2 of the C # operator overload:
Struct Hour {public int iValue; / / constructor public Hour (int initialValue) {this.iValue = initialValue;} / / 1, define operator overload (unary operator) public static Hour operator + + (Hour huValue) {huValue.iValue++; return huValue;} / / C # operator overload / / = = operator is a binary operator and must take two parameters public static bool operator== (Hour lhs,Hour rhs) {return lhs.iValue = = rhs.iValue } public static bool operators = (Hour lhs,Hour rhs) {return lhs.iValue! = rhs.iValue;}} / + + operators take the form of prefixes and suffixes, and C # can intelligently use the same operator for prefixes and suffixes. Static void Main (string [] args) {/ / 2, instantiated class, C # operator overload Hour hu = new Hour (10); / / 3, / / operator overload hu++; / / output result Console.WriteLine ("Hu++ = {0}", hu.iValue); / / C # operator overload + + hu; / / output result Console.WriteLine ("Hu++ = {0}", hu.iValue); Console.ReadKey () } Thank you for your reading. the above is the content of "how to achieve C# operator overloading". After the study of this article, I believe you have a deeper understanding of how to achieve C# operator overloading. Specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.