Http://msdn.microsoft.com/zh-cn/library/ms228593 (vs.80). aspx
C #3.0 specifications and other information can be obtained in http://msdn2.microsoft.com/en-us/vcsharp/aa336745.aspx
Some normative suggestions from fxcop |
|
|
The following is based on fxcop. net Code Write specifications for reference only.
I. Design)
1. abstract types shocould not have Constructors
Abstract classes should not declare Constructor
2. assemblies shocould have valid Strong names
Program Set should have a strong name
3. Avoid empty Interfaces
Avoid using empty Interfaces
4. Avoid excessive parameters on generic types
Avoid using too many type parameters in a generic class
5. Avoid namespaces with few types
Avoid having too few types in the namespace
6. Avoid out parameters
Avoid using out parameters
7. Collections shocould implement generic interface
Set class should implement generic interface
8. consider passing base types as parameters
Use the basic type as a parameter whenever possible
9. Declare Event Handlers correctly
Correctly declare the event processor. The event processor should not have a return value.
10. Declare types in namespaces
Type should be defined in the namespace, rather than outside
11. default parameters shocould not be used
Parameter default value should not be used (C # No parameter default value)
12. Define accessors for Attribute arguments
The accessor should be defined for the constructor parameter of the feature (feature). Its name is different from that of the constructor parameter only in the upper and lower case.
13. Do not catch General Exception types
Do not catch Common exceptions (system. Exception)
14. Do not declare protected members in sealed types
Do not define protected members in the closed type
15. Do not declare static members on generic types
Do not use static members in generic types
16. Do not declare virtual members in sealed types
Do not define virtual members in the closed type
17. Do not declare visible instance fields
Do not define visible (public/internal) instance domain Variables
18. Do not expose generic lists
Do not expose the fan table directly.
19. Do not hide base class methods
Do not hide (use or do not use new) Methods of the base class
20. Do not nest generic types in member signatures
Do not nest a generic class in the member's signature (parameter or return value)
21. Do not override operator equals on reference types
Do not overload the = operator in the reference type
22. Do not pass types by reference
Do not use the reference (ref or out) Transfer Type
23. Enum storage shocould be int32
Enumeration should be int32 type
24. Enumerators shocould be stronugly typed
The Enumerator should be strongly typed.
25. enums shocould have zero value
Enumeration should have a value of 0
26. generic methods shold provide type parameter
The method of generic classes should provide type parameters.
27. icollection implementations have stronugly typed members
A strongly typed member should be used in the implementation of the Set interface.
28. Implement standard exception Constructors
Custom exceptions should implement four standard constructor methods for exception classes
29. indexers shocould not be multidimensional
Indexes should not be multidimensional.
30. interface methods shocould be callable by child types
Interface methods should be called by quilt class
31. Lists are stronugly typed
The table should be strongly typed.
32. Mark assemblies with assembly version
Use the assembly version to mark the Assembly
33. Mark assemblies with clscompliant
Use the clscompliant feature to mark the Assembly
34. Mark assemblies with comvisible
Use the system. runtime. interopservices. comvisibleattribute feature to mark the Assembly
35. Mark attributes with attributeusageattribute
Use attributeusageattribute to identify feature classes
36. Mark enums with flagsattribute
Enumeration with a combination should be marked with the flagsattribute feature; otherwise
37. Members shocould not expose certain concrete types
The specific type of a member (return value or parameter) should not be exposed. Use the interface whenever possible.
38. Move pinvokes to native Methods class
Move the call to the local method class (not very understandable)
39. nested types shocould not be visible
The nested type should not be visible.
40. Override methods on comparable types
Methods such as equals should be rewritten for comparable types
41. Override operator equals on overriding add and subtract
During the rewrite + and-operations, the = operator should be rewritten at the same time.
42. properties shocould not be write only
Attribute should not be written only
43. Provide obsoleteattribute message
Outdated members should use the obsoleteattribute feature and provide the corresponding message to prompt the user.
44. replace repetitive arguments with Params Array
Replace repeated parameters with parameter Arrays
45. Static holder types shoshould be sealed
Only types containing static members should be declared as closed
46. Static holder types shocould not have Constructors
Only types containing static members should have constructor Methods
47. String URI overloads call system URI overloads
The overload of URI parameters of string type should call the system's overload of using URI parameters
48. types shocould not extend certain base types
The type should not be inherited from a specific class (a derived class). For example, the exception class should not be inherited from applicationexception, but should be inherited from system. Exception.
49. types that own disposable fields shocould be disposable
The types containing releasable members should be releasable (implementing the idisposable Interface)
50. types that own native resources shoshould be disposable
The types of unmanaged resources used should be releasable (implementing the idisposable Interface)
51. URI parameters shocould not be strings
The URI parameter should not be of the string type.
52. Uri properties shocould not be strings
The URI attribute should not be of the string type.
53. Uri return values shocould not be strings
The returned value of the URI type should not be of the string type.
54. Use events where appropriate
Use events as appropriate
55. use generic event handler instances
Use a generic event processor instance
56. Use generics where appropriate
Use the model when appropriate
57. Use integral or string argument for indexers
The indexer should use integer or string-type parameters.
58. Use properties where appropriate
Use attributes when appropriate (instead of Methods Starting with get or set)
59. Validate arguments of public methods
The public method parameters should be checked at the beginning of the method (for example, whether it is null)
Ii. Globalization)
1. Avoid duplicate Accelerators
Avoid repeated shortcut keys (acceleration keys) in top-level controls)
2. Do not hardcode locale specific strings
Do not hard encode special local strings (such as special system paths ).
3. Do not pass literals as localized Parameters
Do not pass the text directly as a parameter that requires localization (use resource files whenever possible)
4. Set locale for Data Types
Set region and language attributes for some data types (locale attributes of dataset and datatable)
5. Specify cultureinfo
Specify the cultural information (Region and Language Information). When calling a method that accepts the system. Globalization. cultureinfo type parameter, it should pass the Cultural Information
6. Specify iformatprovider
Specify the format provider. When calling a method that accepts system. iformatprovider-type parameters, you must pass the format provider.
7. Specify messageboxoptions
Specify the MessageBox option. when calling the MessageBox. Show method, system. Windows. Forms. messageboxoptions should be passed, especially in some areas that are used to reading from right to left.
Iii. Interoperability)
1. Auto layout types shocould not be comvisible
The Automatic Layout type should not be visible to com (SET system. runtime. interopservices. comvisibleattribute to false)
2. Avoid int64 arguments for VB6 clients
Avoid int64 type. If a Member may be called by Visual Basic 6 COM clients
3. Avoid non-public fields in comvisible value types
Avoid including non-public instance domains in a value type marked with the comvisible feature
4. Avoid overloads in comvisible Interfaces
Avoid declaring overloading in an interface with the comvisible feature
5. Avoid static members in comvisible types
Avoid marking a type with the comvisible feature
6. Call getlasterror immediately after pinvoke
Use getlasterror to read the error message immediately after pinvoke.
7. com registration methods shoshould be matched
Com registration method (the method marked with the system. runtime. interopservices. comregisterfunctionattribute feature) should be paired (a method marked with system. runtime. interopservices. comunregisterfunctionattribute matches with it)
8. com registration methods shocould not be visible
The com registration method should be invisible.
9. com visible type base types shoshould be comvisible
Base classes marked with the comvisible feature should also be inherited from classes marked with the comvisible feature
10. com visible types shocould be creatable
The types marked with the comvisible feature should be constructed using the default constructor.
11. Declare pinvokes correctly
Correctly define pinvokes
12. Do not use autodual classinterfacetype
Do not set the value of the system. runtime. interopservices. classinterfaceattribute attribute to system. runtime. interopservices. classinterfacetype. autodual.
13. Mark Boolean pinvoke arguments with specified alas
The pinvoke parameter of the boolean type should be marked with the system. runtime. interopservices. marshalasattribute feature.
14. Mark comsource interfaces as idispatch
Mark system. runtime. interopservices. comsourceinterfacesattribute as system. runtime. interopservices. cominterfacetype. interfaceisidispatch.
15. pinvoke entry points shold exist
Pinvoke should have an entry point
16. pinvokes shocould not be visible
Pinvoke should be visible
Iv. Naming)
1. Avoid language specific type names in Parameters
Avoid using the type related to a specific language in the parameter (use uint16 instead of ushort)
2. Avoid type names in Parameters
Avoid using the type name in externally visible Parameters
3. Compound Words shocould be cased correctly
Compound Words should use the correct case (do not write mutlipart as multipart, or write toolbar as toolbar, and filename as filename)
4. Do not name enum values 'reserved'
Do not use reserved words in enumeration values
5. Do not prefix enum values with type name
Do not use a type prefix in the enumerated values (for example, do not use a digital prefix)
6. Events shocould not have before or after prefix
The event name should not contain the before and after prefixes (use the suffix of ing and Ed whenever possible)
7. Flags enums shocould have plural names
Enumeration marked with the system. flagsattribute feature should use names in the plural form
8. Identifiers shocould be cased correctly
The identifier (namespace, class name, attribute name, Interface Name, method name, and so on) should use the correct case (usually starts with an uppercase letter, and each word in the future is capitalized)
9. Identifiers shocould be spelled correctly
The identifier should be correctly divided into different words.
10. Identifiers shocould differ by more than case
The identifier should be case-insensitive)
11. Identifiers shocould have correct prefix
The identifier should use the correct prefix (the interface should start with the letter I)
12. Identifiers shocould have correct suffix
The identifier should use the correct suffix
System. Attribute/attribute
System. eventargs/eventargs
System. Exception/exception
System. Collections. icollection/Collection
System. Collections. idictionary/Dictionary
System. Collections. ienumerable/Collection
System. Collections. Queue/collection or queue
System. Collections. Stack/collection or stack
System. Collections. Generic. icollection/Collection
System. Collections. Generic. idictionary/Dictionary
System. Data. dataset/Dataset
System. Data. datatable/collection or datatable
System. Io. Stream/stream
System. Security. ipermission/permission
System. Security. Policy. imembershipcondition/Condition
An event-handlerdelegate./eventhandler
13. Identifiers shoshould not contain underscores
Underlines should not be used for the identifier
14. Identifiers shocould not have incorrect prefix
The identifier should not use an incorrect prefix (for example, a letter should not be used as the prefix)
15. Identifiers shocould not have incorrect suffix
Incorrect suffixes should not be used for the identifier (do not use the suffix names mentioned in 12 in incorrect places, and suffix names such as delegate, Enum, flags for an enumeration, and impl)
16. Identifiers shocould not match keywords
The identifier should not conflict with the system keyword.
17. Long acronyms shoshould be Pascal-cased
For acronyms with a length greater than or equal to 3, use Pascal's naming rules, that is, uppercase letters.
18. Only flagsattribute enums shocould have plural names
Only the enumerated names marked with the "system. flagsattribute" feature should use the plural number. Otherwise, the singular number should be used.
19. parameter names shocould match base Declaration
The Parameter Name of the derived item should be consistent with the base item (the method of rewriting or implementation of the derived class should have the same parameter name as the base item)
20. parameter names shocould not match Member names
The parameter names of methods should not be the same as those of similar methods or interfaces.
21. Property names shocould not match get Methods
The attribute name should not be the same as the second half of the method name starting with get.
22. Resource string Compound Words shoshould be cased correctly
The resource string containing the words should use the correct case (the first letter of each word is capitalized)
23. Resource strings shocould be spelled correctly
The resource string should be correctly spelled
24. Short acronyms shocould be uppercase
Short acronyms should all be capitalized (such as DB and Cr)
25. type names shocould not match namespaces
The type name should not be the same as the namespace name
26. Use preferred terms
Some projects or names are preferred.
Complus/enterpriseservices
Cancelled/canceled
Indices/Indexes
Login/logon
Logout/Logoff
Signon/signin
Signoff/signout
Writeable/writable
V. Performance (performance rules)
1. Avoid cballs that require unboxing
Avoid calling a method. It returns the object type, and you need a value type (you need to unpack the returned value)
2. avoid costly cballs where possible
Avoid Expensive calls as much as possible
3. Avoid excessive locals
Avoid using too many local variables (more than 64, part of which may be generated by the compiler)
4. Avoid uncalled private code
Avoid declaring private and internal members that have never been called in the Assembly, except below:
Clear interface member
Static Constructor
Static main method (excluding parameters or containing only parameters of a string array)
Serialization Constructor
Marked with system. runtime. interopservices. comregisterfunctionattribute or system. runtime. interopservices. comunregisterfunctionattribute.
Override Method
5. Avoid uninstantiated internal classes
Avoid declaring internal classes that will not be instantiated, except in the following cases
Value Type
Abstract type
Enumeration
Delegate
Array type generated by the compiler
Internal class containing only static members
6. Avoid unnecessary string Creation
Avoid creating unnecessary string instances (refer to 'string' created through tolower and toupper), including the following situations
Tolower and toupper are called multiple times for the same string instance (we recommend that you assign the return value to a local variable and then use this local variable)
Use equals, '= ',! = Compare 'string' created through tolower and toupper (recommended: String. Compare)
To a system. collections. specialized. hybriddictionary type members pass 'string' created through tolower and toupper (recommended: hybriddictionary has a constructor overload that indicates whether to ignore case-sensitive parameters, use this overload and pass a true value)
7. Avoid unsealed attributes
Avoid declaring unclosed properties (recommended: declared as sealed/NotInheritable-vb.net or abstract)
8. Avoid unused Parameters
Avoid including unused parameters in the method declaration, except in the following cases
Proxy Reference Method
Method used as an event handler
Abstract)
Virtual)
Override)
External method (extern)
9. Dispose Methods shocould call suppressfinalize
The dispose method should call suppressfinalize to request the system not to call its Finalize method
10. Do not call properties that clone values in Loops
Do not use 'Return the attributes of a clone object' in a loop (each return 'reference a different 'object will lead to the creation of a large number of identical objects)
11. Do not cast unnecessarily
Do not perform unnecessary type conversion (especially for trial conversions. We recommend that you use the is operator to determine whether the conversion is successful before conversion)
12. Do not concatenate strings inside Loops
Do not concatenate strings in a loop (we recommend that you use stringbuilder instead of string)
13. Do not ignore Method Results
Do not ignore the return value of the method (a new string is usually returned when the string method is called)
14. Do not initialize unnecessarily
Do not perform unnecessary initialization (for example, initializing a class member as its default value)
15. initialize reference type static fields inline
Initialize the static member declaration directly or call the static method initialization (do not use the static constructor to initialize the static member. The static constructor will affect the performance), except in the following cases:
Initialization has a high impact on the global status, and the type does not need to be initialized before use.
If you do not need to access this type of static members, the global status can be accessed.
16. Override equals and operator equals on value types
For public value types, rewrite the equals method and the '=' operator (if you want the user to compare or sort instances or use them as the key of the hash table)
17. Prefer Jagged arrays over multidimenys
Replace the multi-dimensional array with a sawtooth array (when the length of each element in the array may be inconsistent)
Note: The common language specification (CLS) does not support sawtooth arrays.
18. properties shocould not return Arrays
Attribute of the Public type should not return an array (the attribute of the array type cannot be write-protected, even if it is read-only, it will confuse the caller unless different copies are returned each time. Suggestion: change to method)
19. Remove empty finalizers
Remove an empty Terminator
20. Remove unused locals
Remove unused local variables
21. Test for empty strings using String Length
Use the Length attribute to test whether the string is null (reason: Do not use = ", = string .. empty, equals ("") and other methods, the efficiency of using the Length attribute is the highest; null = empty does not throw an exception; in dotnetframework2, you can use the isnullorempty method to determine whether the string is null or empty)
22. Use literals where appropriate
Use const instead of static readonly when appropriate (const is assigned during compilation)