กิจกรรม
17 มี.ค. 21 - 21 มี.ค. 10
สร้างแอป AI และตัวแทน เข้าร่วมชุด meetup เพื่อสร้างโซลูชัน AI ที่ปรับขนาดได้ตามกรณีการใช้งานจริงกับนักพัฒนาและผู้เชี่ยวชาญร่วมกัน
ลงทะเบียนตอนนี้เบราว์เซอร์นี้ไม่ได้รับการสนับสนุนอีกต่อไป
อัปเกรดเป็น Microsoft Edge เพื่อใช้ประโยชน์จากคุณลักษณะล่าสุด เช่น การอัปเดตความปลอดภัยและการสนับสนุนด้านเทคนิค
Property | Value |
---|---|
Rule ID | CA2225 |
Title | Operator overloads have named alternates |
Category | Usage |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | No |
An operator overload was detected and the expected named alternative method was not found.
By default, this rule only looks at externally visible types, but this is configurable.
Operator overloading allows the use of symbols to represent computations for a type. For example, a type that overloads the plus symbol +
for addition would typically have an alternative member named Add
. The named alternative member provides access to the same functionality as the operator. It's provided for developers who program in languages that do not support overloaded operators.
This rule examines:
Implicit and explicit cast operators in a type by checking for methods named To<typename>
and From<typename>
.
The operators listed in the following table:
C# | Visual Basic | C++ | Alternate method name |
---|---|---|---|
+ (binary) | + | + (binary) | Add |
+= | += | += | Add |
& | And | & | BitwiseAnd |
&= | And= | &= | BitwiseAnd |
| | Or | | | BitwiseOr |
|= | Or= | |= | BitwiseOr |
-- | N/A | -- | Decrement |
/ | / | / | Divide |
/= | /= | /= | Divide |
== | = | == | Equals |
^ | Xor | ^ | Xor |
^= | Xor= | ^= | Xor |
> | > | > | CompareTo or Compare |
>= | >= | >= | CompareTo or Compare |
++ | N/A | ++ | Increment |
!= | <> | != | Equals |
<< | << | << | LeftShift |
<<= | <<= | <<= | LeftShift |
< | < | < | CompareTo or Compare |
<= | <= | <= | CompareTo or Compare |
&& | N/A | && | LogicalAnd |
|| | N/A | || | LogicalOr |
! | N/A | ! | LogicalNot |
% | Mod | % | Mod or Remainder |
%= | N/A | %= | Mod |
* (binary) | * | * | Multiply |
*= | N/A | *= | Multiply |
~ | Not | ~ | OnesComplement |
>> | >> | >> | RightShift |
>>= | N/A | >>= | RightShift |
- (binary) | - (binary) | - (binary) | Subtract |
-= | N/A | -= | Subtract |
true | IsTrue | N/A | IsTrue (Property) |
- (unary) | N/A | - | Negate |
+ (unary) | N/A | + | Plus |
false | IsFalse | False | IsTrue (Property) |
*N/A means the operator cannot be overloaded in the selected language.
หมายเหตุ
In C#, when a binary operator is overloaded, the corresponding assignment operator, if any, is also implicitly overloaded.
To fix a violation of this rule, implement the alternative method for the operator. Name it using the recommended alternative name.
Do not suppress a warning from this rule if you're implementing a shared library. Applications can ignore a warning from this rule.
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA2225
// The code that's violating the rule is on this line.
#pragma warning restore CA2225
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA2225.severity = none
For more information, see How to suppress code analysis warnings.
Use the following option to configure which parts of your codebase to run this rule on.
You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Usage) that it applies to. For more information, see Code quality rule configuration options.
You can configure which parts of your codebase to run this rule on, based on their accessibility, by setting the api_surface option. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:
dotnet_code_quality.CAXXXX.api_surface = private, internal
หมายเหตุ
Replace the XXXX
part of CAXXXX
with the ID of the applicable rule.
The following example defines a structure that violates this rule. To correct the example, add a public Add(int x, int y)
method to the structure.
public struct Point
{
private int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return String.Format("({0},{1})", x, y);
}
// Violates rule: OperatorOverloadsHaveNamedAlternates.
public static Point operator +(Point a, Point b)
{
return new Point(a.x + b.x, a.y + b.y);
}
public int X { get { return x; } }
public int Y { get { return x; } }
}
คำติชม .NET
.NET เป็นโครงการโอเพนซอร์ส เลือกลิงก์เพื่อให้คำติชม:
กิจกรรม
17 มี.ค. 21 - 21 มี.ค. 10
สร้างแอป AI และตัวแทน เข้าร่วมชุด meetup เพื่อสร้างโซลูชัน AI ที่ปรับขนาดได้ตามกรณีการใช้งานจริงกับนักพัฒนาและผู้เชี่ยวชาญร่วมกัน
ลงทะเบียนตอนนี้