XJS155-8301 / 1.3 Coding Standard: C# Page 5 of 48
For External Use 2005-05-19
TRAD Philips’ proprietary, © 2003 Philips Electronics N.V. All rights reserved
7.2.4. Rec. 7@201: Selection statements (if-else and switch) should be used when the control
flow depends on an object’s value; dynamic binding should be used when the control flow
depends on the object’s type.................................................................................................... 29
7.2.5. Rule 7@301: All variants of an overloaded method shall be used for the same purpose and have
similar behavior...................................................................................................................... 29
7.2.6. Rec. 7@303: If you must provide the ability to override a method, make only the most complete
overload virtual and define the other operations in terms of it................................................... 29
7.2.7. Rec. 7@401: Specify methods using preconditions, postconditions, exceptions; specify classes
using invariants....................................................................................................................... 30
7.2.8. Rec. 7@402: Use C# to describe preconditions, postconditions, exceptions, and class invariants.30
7.2.9. Rule 7@403: It shall be possible to use a reference to an object of a derived class wherever a
reference to that object’s base class object is used.................................................................... 31
7.2.10. Rec. 7@501: Do not overload any ‘modifying’ operators on a class type. ............................ 31
7.2.11. Rule 7@502: Do not modify the value of any of the operands in the implementation of an
overloaded operator................................................................................................................. 31
7.2.12. Rec. 7@503: If you implement one of operator==(), the Equals method or
GetHashCode(), implement all three. ................................................................................ 31
7.2.13. Rec. 7@504: Use a struct when value semantics are desired............................................... 31
7.2.14. Rule 7@601: Allow properties to be set in any order. .............................................................. 32
7.2.15. Rec. 7@602: Use a property rather than a method when the member is a logical data member.. 32
7.2.16. Rec. 7@603: Use a method rather than a property when this is more appropriate. .................... 32
7.2.17. Rule 7@604: Do not create a constructor that does not yield a fully initialized object............... 32
7.2.18. Rule 7@608: Always check the result of an as operation........................................................ 32
7.2.19. Rec. 7@610: Use explicit interface implementation only to prevent name-clashing or to support
optional interfaces................................................................................................................... 32
8. Exceptions ...........................................................................................................34
8.1. Overview................................................................................................................................ 34
8.2. Rules and Recommendations................................................................................................... 34
8.2.1. Rule 8@101: Only throw exceptions in exceptional situations.................................................. 34
8.2.2. Rule 8@102: Do not throw exceptions from inside destructors................................................. 34
8.2.3. Rec. 8@103: Only re-throw exceptions when you want to specialize the exception................... 34
8.2.4. Rule 8@104: List the explicit exceptions a method or property can throw................................ 34
8.2.5. Rule 8@105: Always log that an exception is thrown. ............................................................. 35
8.2.6. Rec. 8@106: Allow callers to prevent exceptions by providing a method or property that returns
the object’s state. .................................................................................................................... 35
8.2.7. Rec. 8@107: Use standard exceptions..................................................................................... 35
8.2.8. Rec. 8@108: Throw informational exceptions. ........................................................................ 35
8.2.9. Rule 8@109: Throw the most specific exception possible. ....................................................... 35
8.2.10. Rule 8@110: Only catch the exceptions explicitly mentioned in the documentation................... 35
8.2.11. Rule 8@201: Derive custom exceptions from ApplicationException......................... 36
8.2.12. Rec. 8@202: Provide common constructors for custom exceptions. ......................................... 36
8.2.13. Rule 8@203: Avoid side-effects when throwing recoverable exceptions. .................................. 36
8.2.14. Rule 8@204: Do not throw an exception from inside an exception constructor. ........................ 36
9. Delegates and events...........................................................................................37
9.1. Overview................................................................................................................................ 37
9.2. Rules and Recommendations................................................................................................... 37
9.2.1. Rule 9@101: Do not make assumptions on the object’s state after raising an event................... 37
9.2.2. Rule 9@102: Always document from which thread an event handler is called........................... 37
9.2.3. Rec. 9@103: Raise events through a protected virtual method.................................................. 37
9.2.4. Rule 9@104: Use the sender/arguments signature for event handlers........................................ 38
9.2.5. Rec. 9@105: Implement add/remove accessors if the number of handlers for an event must be
limited. ................................................................................................................................... 38
评论3
最新资源