Compile High-Quality code: 157 suggestions for improving C # program download source code

Source: Internet
Author: User

====

Directory

Preface
Part 1 Language
Chapter 2 Basic language elements/2
Suggestion 1: Correct string operation/2
Recommendation 2: use the default transformation method/6
Recommendation 3: differentiate between forced transformation and as and is/9
Suggestion 4: TryParse is better than Parse/12
Suggestion 5: Use int? To ensure that the value type can also be null/15
Recommendation 6: Use readonly and const differently/16
Suggestion 7: use 0 as the default enumeration value/19
Recommendation 8: Avoid providing explicit values/20 for enumeration Elements
Recommendation 9: Used to overload operators/22
Recommendation 10: consider whether to implement comparator/23 when creating an object
Recommendation 11: differential treatment = and Equals/27
Recommendation 12: rewrite GetHashCode/29 when rewriting Equals.
Suggestion 13: format the string for Type output/32
Recommendation 14: Correct implementation of shortest copy and deep copy/36
Suggestion 15: use dynamic to simplify reflection Implementation/40


Chapter 4 collections and LINQ/43
Suggestion 16: array/43 should not be used when the number of elements is variable
Recommendation 17: Use foreach for loop traversal in most cases/45
Suggestion 18: foreach cannot replace for/51
Recommendation 19: use more effective objects and collections for initialization/53
Suggestion 20: Use a generic set instead of a non-generic set/54
Suggestion 21: select the correct set/57
Recommendation 22: Ensure the thread security of the Set/61
Recommendation 23: Avoid using List as the base class of the custom collection class/64
Suggestion 24: The iterator should be read-only/67
Suggestion 25: exercise caution when performing write operations on set attributes/68
Recommendation 26: Use the anonymous type to store the results of the LINQ query/70
Recommendation 27: Use Lambda expressions/73 in queries
Suggestion 28: understand the difference between latency and active value/75
Recommendation 29: differentiate IEnumerable and IQueryable/78 in a LINQ Query
Suggestion 30: Replace the comparator and iterator/80 in the set with LINQ
Recommendation 31: Avoid unnecessary iterations in the LINQ query/83


Chapter 2 generic, delegated, and event/86
Recommendation 32: always give priority to generics/86
Suggestion 33: avoid declaring static members/88 in generic type
Recommendation 34: Set constraints for generic parameters/90
Suggestion 35: use default to specify the initial value for a generic variable/92
Suggestion 36: Use the delegation statement in FCL/94
Recommendation 37: Use Lambda expressions instead of methods and anonymous methods/96
Suggestion 38: Beware of traps in closures/99
Recommendation 39: understand the essence of Delegation/103
Suggestion 40: Use the event keyword to apply protection to the Delegate/106
Recommendation 41: Implement standard event model/108
Recommendation 42: use generic parameters to be compatible with the non-variability of generic interfaces/109
Suggestion 43: Make the generic parameters in the interface support covariant/111
Suggestion 44: Understand the co-change in Delegation/112
Recommendation 45: Specify the inverter/114 for generic parameters


Chapter 4 Resource Management and serialization/4th
Suggestion 46: The interface IDisposable/116 must be inherited to explicitly release resources
Recommendation 47: provide implicit cleanup/119 in the terminator even if an explicit release method is provided
Suggestion 48: The Dispose method should be allowed to be called multiple times/120
Suggestion 49: extract a protected virtual method/121 in Dispose Mode
Suggestion 50: In Dispose mode, managed resources and unmanaged resources should be treated differently/123
Suggestion 51: the type with releasable fields or local resources should be releasable/124
Suggestion 52: release resources in time/125
Suggestion 53: if necessary, assign null/127 to the reference of an object that is no longer in use.
Suggestion 54: useless field annotation not serializable/131
Recommendation 55: use custom features to reduce serializable fields/136
Recommendation 56: Use the inherited ISerializable interface to control the serialization process more flexibly/137
Recommendation 57: the sub-type implementing ISerializable should be responsible for serialization of the parent class/140


Chapter 4 Exceptions and custom exceptions/5th
Suggestion 58: Use throwing an exception instead of returning the error code/144
Recommendation 59: do not cause exceptions in inappropriate situations/147
Suggestion 60: Use Inner Exception/150 when an Exception is thrown again
Suggestion 61: Avoid writing Invalid code in finally/151
Suggestion 62: Avoid nested exceptions/157
Recommendation 63: Avoid "eat" exceptions/160
64 recommended: add the Tester-Doer mode for the loop instead of placing try-catch in the loop/161
Recommendation 65: always handle uncaptured exceptions/162
Recommendation 66: correctly capture exceptions in multiple threads/166
Recommendation 67: use custom exceptions with caution/168
Recommendation 68: derive an Exception from System. Exception or other common basic exceptions/170
Recommendation 69: Use finally to avoid resource leakage/172
Recommendation 70: Avoid record exceptions at lower call stacks/175


Chapter 2 Asynchronization, multithreading, task and parallelism/6th
Recommendation 71: differentiate asynchronous and multithreading application scenarios/177
Recommendation 72: Use semaphores/180 in thread synchronization
Recommendation 73: avoid locking inappropriate synchronization objects/184
Recommendation 74: guard against thread IsBackground/188
Recommendation 75: Be alert that the thread will not start immediately/189
Recommendation 76: Be alert to thread priority/191
Suggestion 77: properly stop the thread/193
Recommendation 78: avoid excessive threads/194
Recommendation 79: Use ThreadPool or BackgroundWorker to replace Thread/196
Recommendation 80: Use Task instead of ThreadPool/198
Recommendation 81: use Parallel to simplify the use of tasks in synchronization status/202
Recommendation 82: Parallel is simplified but not the same as default Task behavior/204
Recommendation 83: Be careful with the traps in Parallel/205
Recommendation 84: Use PLINQ/208
Recommendation 85: Exception Handling in Task/209
Recommendation 86: Exception Handling in Parallel/214
Suggestion 87: differentiate the thread model of WPF and WinForm/216
Recommendation 88: parallel operations are not always faster/220
89: exercise caution when using the lock/222 in the parallel method body


Part 2 Architecture
Chapter 2 member design/7th
Recommendation 90: do not provide public constructors for abstract classes/226
91: visible fields should be reconstructed to properties/226
Recommendation 92: exercise caution when using arrays or collections as attributes/227
Recommendation 93: primary attributes and fields should be initialized in the constructor/228
Suggestion 94: Treat override and new/229 differently
Suggestion 95: Avoid calling virtual members/235 in the constructor
Recommendation 96: members should give priority to the disclosure of the base type or interface/236
Recommendation 97: Pass the base type or interface as a parameter first/237
Recommendation 98: Use params to reduce repeated parameters/237
Recommendation 99: Do not use subclass parameter/238 during Rewriting
Recommendation 100: there is no difference between the static method and the instance method/239
Recommendation 101: use an extension method to add a method to an existing type/240


Chapter 2 type design/8th
Recommendation 102: application scenarios that distinguish interfaces from abstract classes/243
Recommendation 103: application scenarios that distinguish combination and Inheritance/245
Suggestion 104: replace conditional statement with polymorphism/248
Recommendation 105: Use a private constructor to enhance Singleton/251
Recommendation 106: Add a static constructor to a static class/253
Recommendation 107: differentiate static classes and Singleton/255
Suggestion 108: identify the type as sealed/255
Recommendation 109: Use Nested classes with caution/256
Recommendation 110: Use a class to replace enum/257
Suggestion 111: Avoid bidirectional coupling/260
Suggestion 112: Abstract objects in the real world into classes and enclose reusable objects as namespaces/262


Chapter 4 Security Design/9th
Suggestion 113: before declaring a variable, consider the maximum value/264
Suggestion 114: MD5 is no longer secure/265
Recommendation 115: verify if the file is tampered with by using HASH/268
Recommendation 116: Avoid using asymmetric algorithms to encrypt files/269
Suggestion 117: use SSL to ensure data security in communications/273
Suggestion 118: Use SecureString to save secret strings such as keys/284
119 recommended: Do not use your own encryption algorithm/289
Recommendation 120: specify a strong name for the assembly/289
Suggestion 121: Set the running permission for the application/291


Part 3 coding specifications and habits
Chapter 2 naming rules/10th
Recommendation 122: use. As the namespace/296
Recommendation 123: the Assembly does not have to have the same name as the namespace/296
Suggestion 124: use the plural number/297 In The namespace
Recommendation 125: Do not use the FCL type name to name your own type. // 297
Suggestion 126: group the type with nouns and nouns/298
Suggestion 127: use an adjective group to name an interface/299
Suggestion 128: consider putting the name of the derived class with the base class name as the suffix/300
Suggestion 129: generic type parameters should be prefixed with T/300
Suggestion 130: Name the enumeration type in the plural and name the enumeration element in the singular/301
Suggestion 131: Use PascalCasing to name the public element/302
Recommendation 132: consider using the class name as the attribute name/302
133: Use camelCasing to name private fields and local variables/303
Suggestion 134: Use prefix/304 with conditions
Recommendation 135: use affirmative phrases to name Boolean attributes/305
Suggestion 136: prefix the suffix to indicate a new version of the existing type/306
Suggestion 137: Add a superior suffix/307 to delegate and event types
Suggestion 138: use verb or adjective phrase to name events and delegate variables/308
Recommendation 139: Event processor naming in combination/309


Chapter 5 code cleanliness/11th
140 recommended: use the default access modifier/311
Suggestion 141: if you do not know whether to use braces, use/312.
Recommendation 142: always provide meaningful names/314
Suggestion 143: The method abstraction level should be at the same level/315
Recommendation 144: Only one thing for one method/316
Suggestion 145: Avoid too long methods and too long classes/317
Recommendation 146: only publish necessary operations/318
Recommendation 147: refactor multiple related attributes into a class/319
Suggestion 148: No repeated code/320
Suggestion 149: use the table driver method to avoid too long if and switch branches/321
Suggestion 150: use an anonymous method or Lambda expression instead of a method/324
Suggestion 151: Use the event accessor to replace the public event member variable/325
Suggestion 152: Least, or even not comment/326
Suggestion 153: if an exception is thrown, note/326.


Chapter 4 standardized development behavior/12th
Recommendation 154: Do not over-design, experience the fun of restructuring in agility/327
Suggestion 155: Submit the unit test code with the production code/336
Recommendation 156: use features to provide multiple versions for the application/342
Recommendation 157: perform automated testing from the first interface./344

====

 

Source code download

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.