当前位置:网站首页>C operator overloads the query table
C operator overloads the query table
2022-07-01 18:23:00 【KeithPro】
There is a problem in the process of project development , The team previously made a lot of non empty judgments on a very low-level base class when writing programs , Later, in the later development, it was found that this kind of unreliable , There is a problem , Need to replace all , wow , When will thousands of files have to be found , Finally, one of them proposed a method , Overload the base class judgment method , The author found that some operations can be overloaded , Some operations cannot be overloaded , Here is a record of the overloaded operator table officially provided by Microsoft , For later use , Also prevent yourself from forgetting ( Now this memory is far from good )
User defined types can overload predefined C# Operator . in other words , When one or both operands are of a certain type , This type provides a custom implementation of the operation . Overloadable operators What is introduced in section C# Operators can be overloaded .
Use operator Keyword to declare operators . Operator declarations must conform to the following rules :
- Also include
publicandstaticModifier . - Unary operator has an input parameter . Binary operators have two input parameters . In each case , Must have at least one parameter of type
TorT?, amongTIs a type that contains operator declarations .
The following example defines a simple structure for representing rational numbers . This structure will overload some Arithmetic operator :
using System;
public readonly struct Fraction
{
private readonly int num;
private readonly int den;
public Fraction(int numerator, int denominator)
{
if (denominator == 0)
{
throw new ArgumentException("Denominator cannot be zero.", nameof(denominator));
}
num = numerator;
den = denominator;
}
public static Fraction operator +(Fraction a) => a;
public static Fraction operator -(Fraction a) => new Fraction(-a.num, a.den);
public static Fraction operator +(Fraction a, Fraction b)
=> new Fraction(a.num * b.den + b.num * a.den, a.den * b.den);
public static Fraction operator -(Fraction a, Fraction b)
=> a + (-b);
public static Fraction operator *(Fraction a, Fraction b)
=> new Fraction(a.num * b.num, a.den * b.den);
public static Fraction operator /(Fraction a, Fraction b)
{
if (b.num == 0)
{
throw new DivideByZeroException();
}
return new Fraction(a.num * b.den, a.den * b.num);
}
public override string ToString() => $"{num} / {den}";
}
public static class OperatorOverloading
{
public static void Main()
{
var a = new Fraction(5, 4);
var b = new Fraction(1, 2);
Console.WriteLine(-a); // output: -5 / 4
Console.WriteLine(a + b); // output: 14 / 8
Console.WriteLine(a - b); // output: 6 / 8
Console.WriteLine(a * b); // output: 5 / 8
Console.WriteLine(a / b); // output: 10 / 4
}
}| Operator | Heavy duty |
|---|---|
| +x、-x、!x、~x、++、--、true、false | These unary operators can be overloaded . |
| x + y、x - y、x * y、x / y、x % y、x & y、x | y、x ^ y、x << y, x >> y、x == y、x != y、x < y, x > y、x <= y, x >= y | These binary operators can be overloaded . Some operators must be overloaded in pairs ; For more information , Please check the notes at the back of this form . |
| x && y、x || y | Cannot overload conditional logical operators . however , If you have overloaded true and false Operator The type of is also overloaded in some way & or | Operator , Can be calculated separately for this type of operand && or || Operator . For more information , see also C# language norm Of User defined conditional logical operators part . |
| a[i], a?[i] | Element access is not considered a reloadable operator , But you can define Indexer . |
| (T)x | Cast operators cannot be overloaded , But you can define custom type conversions that can be performed by cast expressions . For more information , see also User defined conversion operators . |
| +=、-=、*=、/=、%=、&=、|=、^=、<<=, >>= | Compound assignment operators cannot be explicitly overloaded . But when overloading binary operators , The corresponding compound assignment operator will also be implicitly overloaded ( If you have any ). for example , Use +( Can be overloaded ) Calculation +=. |
| ^x、x = y、x.y、x?.y、c ? t : f、x ?? y、x ??= y、x..y、x->y、=>、f(x)、as、await、checked、unchecked、default、delegate、is、nameof、new、sizeof、stackalloc、switch、typeof、with | These operators cannot be overloaded . |
remarks
Comparison operators must be overloaded in pairs . in other words , If you overload any one of a pair of operators , Then the other operator must also be overloaded . Such pairing is shown below :
==and!=Operator<and>Operator<=and>=Operator
边栏推荐
- MySQL -- explain performance optimization
- Wechat applet blind box - docking wechat payment
- Unity3d extended toolbar
- Source code of new campus errand / campus task platform on mutual station
- Detailed explanation of select in golang
- PMP daily three questions (February 15, 2022)
- Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
- To improve the efficiency of office collaboration, trackup may be the best choice
- About selenium element positioning being overwritten
- (6) VIM editor MV cat redirection standard input and output more pipe symbols head tail
猜你喜欢

540. Single element in ordered array

Yuancosmos game farmersworld farmers world - core content of the second conference in China!

How to retrieve the password for opening Excel files

Review Net 20th anniversary development and 51aspx growth

Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash

Data query language (DQL)

An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press

Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day

Growing up in the competition -- (Guangyou's most handsome cub) Pikachu walking

Mujoco model learning record
随机推荐
[CF1476F]Lanterns
New 95 community system whole station source code
Is online stock account opening safe? Is it reliable?
Data query language (DQL)
Rotation order and universal lock of unity panel
transform. Forward and vector3 Differences in the use of forward
Common design parameters of solid rocket motor
Wechat applet blind box - docking wechat payment
Operating system interview assault
Domestic spot silver should be understood
期货先锋这个软件正规吗安全吗?选择哪家期货公司更安全?
Set the style of QT property sheet control
Data warehouse (3) star model and dimension modeling of data warehouse modeling
PMP daily three questions (February 15, 2022)
[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
Small exercise -- subnet division and summary
Rust language - cargo, crates io
What impact will multinational encryption regulation bring to the market in 2022
Fix the problem that easycvr device video cannot be played
Redis主从实现10秒检查与恢复