| MSDN Home > MSDN Library > .NET Development > .NET Framework SDK > .NET Framework > Reference > Design Guidelines for Class Library Developers | |||||||||||||||||||||||||
| .NET Framework General Reference | |||||||||||||||||||||||||
| Operator Overloading Usage Guidelines | |||||||||||||||||||||||||
| The following rules outline the guidelines for operator overloading: | |||||||||||||||||||||||||
| Define operators on value types that are logical built-in language types, such as the System.Decimal Structure. | |||||||||||||||||||||||||
| Provide operator-overloading methods only in the class in which the methods are defined. | |||||||||||||||||||||||||
| Use the names and signature conventions described in the Common Language Specification (CLS). | |||||||||||||||||||||||||
| Use operator overloading in cases where it is immediately obvious what the result of the operation will be. For example, it makes sense to be able to subtract one Time value from another Time value and get a TimeSpan. However, it is not appropriate to use the or operator to create the union of two database queries, or to use shift to write to a stream. | |||||||||||||||||||||||||
| Overload operators in a symmetric manner. For example, if you overload the equality operator (==), you should also overload the not equal operator(!=). | |||||||||||||||||||||||||
| Provide alternate signatures. Most languages do not support operator overloading. For this reason, always include a secondary method with an appropriate domain-specific name that has the equivalent functionality. It is a Common Language Specification (CLS) requirement to provide this secondary method. The following example is CLS-compliant. | |||||||||||||||||||||||||
| [C#] | |||||||||||||||||||||||||
| class Time | |||||||||||||||||||||||||
| { | |||||||||||||||||||||||||
| TimeSpan operator -(Time t1, Time t2) { } | |||||||||||||||||||||||||
| TimeSpan Difference(Time t1, Time t2) { } | |||||||||||||||||||||||||
| } | |||||||||||||||||||||||||
| The following table contains a list of operator symbols and the corresponding alternative methods and operator names. | |||||||||||||||||||||||||
| C++ operator symbol | Name of alternative method | Name of operator | |||||||||||||||||||||||
| Not defined | ToXxx or FromXxx | op_Implicit | |||||||||||||||||||||||
| Not defined | ToXxx or FromXxx | op_Explicit | |||||||||||||||||||||||
| + (binary) | Add | op_Addition | |||||||||||||||||||||||
| - (binary) | Subtract | op_Subtraction | |||||||||||||||||||||||
| * (binary) | Multiply | op_Multiply | |||||||||||||||||||||||
| / | Divide | op_Division | |||||||||||||||||||||||
| % | Mod | op_Modulus | |||||||||||||||||||||||
| ^ | Xor | op_ExclusiveOr | |||||||||||||||||||||||
| & (binary) | BitwiseAnd | op_BitwiseAnd | |||||||||||||||||||||||
| | | BitwiseOr | op_BitwiseOr | |||||||||||||||||||||||
| && | And | op_LogicalAnd | |||||||||||||||||||||||
| || | Or | op_LogicalOr | |||||||||||||||||||||||
| = | Assign | op_Assign | |||||||||||||||||||||||
| << | LeftShift | op_LeftShift | |||||||||||||||||||||||
| >> | RightShift | op_RightShift | |||||||||||||||||||||||
| Not defined | LeftShift | op_SignedRightShift | |||||||||||||||||||||||
| Not defined | RightShift | op_UnsignedRightShift | |||||||||||||||||||||||
| == | Equals | op_Equality | |||||||||||||||||||||||
| > | Compare | op_GreaterThan | |||||||||||||||||||||||
| < | Compare | op_LessThan | |||||||||||||||||||||||
| != | Compare | op_Inequality | |||||||||||||||||||||||
| >= | Compare | op_GreaterThanOrEqual | |||||||||||||||||||||||
| <= | Compare | op_LessThanOrEqual | |||||||||||||||||||||||
| *= | Multiply | op_MultiplicationAssignment | |||||||||||||||||||||||
| -= | Subtract | op_SubtractionAssignment | |||||||||||||||||||||||
| ^= | Xor | op_ExclusiveOrAssignment | |||||||||||||||||||||||
| <<= | LeftShift | op_LeftShiftAssignment | |||||||||||||||||||||||
| %= | Mod | op_ModulusAssignment | |||||||||||||||||||||||
| += | Add | op_AdditionAssignment | |||||||||||||||||||||||
| &= | BitwiseAnd | op_BitwiseAndAssignment | |||||||||||||||||||||||
| |= | BitwiseOr | op_BitwiseOrAssignment | |||||||||||||||||||||||
| , | None assigned | op_Comma | |||||||||||||||||||||||
| /= | Divide | op_DivisionAssignment | |||||||||||||||||||||||
| -- | Decrement | op_Decrement | |||||||||||||||||||||||
| ++ | Increment | op_Increment | |||||||||||||||||||||||
| - (unary) | Negate | op_UnaryNegation | |||||||||||||||||||||||
| + (unary) | Plus | op_UnaryPlus | |||||||||||||||||||||||
| ~ | OnesComplement | op_OnesComplement | |||||||||||||||||||||||
| See Also | |||||||||||||||||||||||||
| Design Guidelines for Class Library Developers | |||||||||||||||||||||||||
| Send comments on this topic. | |||||||||||||||||||||||||
| © 2001 Microsoft Corporation. All rights reserved. | |||||||||||||||||||||||||
| Contact Us | E-Mail this Page | MSDN Flash Newsletter | |||||||||||||||||||||||||
| © 2002 Microsoft Corporation. All rights reserved. Terms of Use Privacy Statement Accessibility | |||||||||||||||||||||||||
| Références générales du .NET Framework | |||||||||||||||||||||||||
| Indications concernant l'utilisation de la surcharge des opérateurs [C#] | |||||||||||||||||||||||||
| Voir aussi | |||||||||||||||||||||||||
| Indications concernant le design pour les développeurs de bibliothèques de classes | |||||||||||||||||||||||||
| Langage | |||||||||||||||||||||||||
| C# | |||||||||||||||||||||||||
| Afficher tout | |||||||||||||||||||||||||
| Les règles suivantes mettent en avant les indications pour la surcharge des opérateurs : | |||||||||||||||||||||||||
| Définissez des opérateurs sur les types valeur qui représentent des types de langage logiques et intégrés, tels que la structure System.Decimal. | |||||||||||||||||||||||||
| Fournissez uniquement des méthodes de surcharge des opérateurs dans la classe où sont définies les méthodes. | |||||||||||||||||||||||||
| Utilisez les noms et les conventions de signature décrits dans la spécification CLS (Common Language Specification). | |||||||||||||||||||||||||
| Utilisez la surcharge des opérateurs dans les cas où le résultat de l'opération est immédiatement connu. Par exemple, il est pertinent de pouvoir soustraire une valeur Time d'une autre et d'obtenir TimeSpan. Cependant, il n'est pas approprié d'utiliser l'opérateur or pour réunir deux requêtes de base de données ou d'utiliser shift pour écrire dans un flux. | |||||||||||||||||||||||||
| Surchargez les opérateurs de manière symétrique. Par exemple, si vous surchargez l'opérateur d'égalité (==), vous devez aussi surcharger l'opérateur différent de (!=). | |||||||||||||||||||||||||
| Fournissez d'autres signatures. La plupart des langages ne prennent pas en charge la surcharge des opérateurs. C'est pourquoi vous devez toujours inclure une méthode secondaire avec un nom spécifique à un domaine qui dispose de la fonctionnalité équivalente. La spécification CLS (Common Language Specification) le requiert pour fournir cette méthode secondaire. L'exemple suivant est conforme CLS. | |||||||||||||||||||||||||
| [C#] | |||||||||||||||||||||||||
| class Time | |||||||||||||||||||||||||
| { | |||||||||||||||||||||||||
| TimeSpan operator -(Time t1, Time t2) { } | |||||||||||||||||||||||||
| TimeSpan Difference(Time t1, Time t2) { } | |||||||||||||||||||||||||
| } | |||||||||||||||||||||||||
| Le tableau suivant contient une liste des symboles d'opérateur, des méthodes de remplacement correspondantes et des noms d'opérateur. | |||||||||||||||||||||||||
| Symbole d'opérateur C++ | Nom de la méthode de remplacement | Nom de l'opérateur | |||||||||||||||||||||||
| Non défini | ToXxx ou FromXxx | op_Implicit | |||||||||||||||||||||||
| Non défini | ToXxx ou FromXxx | op_Explicit | |||||||||||||||||||||||
| + (binaire) | Add | op_Addition | |||||||||||||||||||||||
| - (binaire) | Subtract | op_Subtraction | |||||||||||||||||||||||
| * (binaire) | Multiply | op_Multiply | |||||||||||||||||||||||
| / | Divide | op_Division | |||||||||||||||||||||||
| % | Mod | op_Modulus | |||||||||||||||||||||||
| ^ | Xor | op_ExclusiveOr | |||||||||||||||||||||||
| & (binaire) | BitwiseAnd | op_BitwiseAnd | |||||||||||||||||||||||
| | | BitwiseOr | op_BitwiseOr | |||||||||||||||||||||||
| && | And | op_LogicalAnd | |||||||||||||||||||||||
| || | Or | op_LogicalOr | |||||||||||||||||||||||
| = | Assign | op_Assign | |||||||||||||||||||||||
| << | LeftShift | op_LeftShift | |||||||||||||||||||||||
| >> | RightShift | op_RightShift | |||||||||||||||||||||||
| Non défini | LeftShift | op_SignedRightShift | |||||||||||||||||||||||
| Non défini | RightShift | op_UnsignedRightShift | |||||||||||||||||||||||
| == | Equals | op_Equality | |||||||||||||||||||||||
| > | Compare | op_GreaterThan | |||||||||||||||||||||||
| < | Compare | op_LessThan | |||||||||||||||||||||||
| != | Compare | op_Inequality | |||||||||||||||||||||||
| >= | Compare | op_GreaterThanOrEqual | |||||||||||||||||||||||
| <= | Compare | op_LessThanOrEqual | |||||||||||||||||||||||
| *= | Multiply | op_MultiplicationAssignment | |||||||||||||||||||||||
| -= | Subtract | op_SubtractionAssignment | |||||||||||||||||||||||
| ^= | Xor | op_ExclusiveOrAssignment | |||||||||||||||||||||||
| <<= | LeftShift | op_LeftShiftAssignment | |||||||||||||||||||||||
| %= | Mod | op_ModulusAssignment | |||||||||||||||||||||||
| += | Add | op_AdditionAssignment | |||||||||||||||||||||||
| &= | BitwiseAnd | op_BitwiseAndAssignment | |||||||||||||||||||||||
| |= | BitwiseOr | op_BitwiseOrAssignment | |||||||||||||||||||||||
| , | Aucune méthode assignée | op_Comma | |||||||||||||||||||||||
| /= | Divide | op_DivisionAssignment | |||||||||||||||||||||||
| -- | Decrement | op_Decrement | |||||||||||||||||||||||
| ++ | Increment | op_Increment | |||||||||||||||||||||||
| - (unaire) | Negate | op_UnaryNegation | |||||||||||||||||||||||
| + (unaire) | Plus | op_UnaryPlus | |||||||||||||||||||||||
| ~ | OnesComplement | op_OnesComplement | |||||||||||||||||||||||
| Voir aussi | |||||||||||||||||||||||||
| Indications concernant le design pour les développeurs de bibliothèques de classes | |||||||||||||||||||||||||
| Envoyer vos commentaires à Microsoft | |||||||||||||||||||||||||
| © 2001 Microsoft Corporation. Tous droits réservés. | |||||||||||||||||||||||||