Apuntes
Best Practices C Sharp
Contenido del apunte (1)
Página 1
Effective C# Tips and Tricks a. C# Language Idioms · Use Properties Instead of Accessible Data Members
· Prefer readonly to const.
· Prefer the ‘is’ or ‘as’ Operators to Casts.
· Use Conditional Attributes Instead of #if.
· Always Provide ToString().
· Understand the Relationships Among the Many Different Concepts of Equality (ReferenceEquals(), staticEquals(), instance Equals(), and operator==).
· Understand the Pitfalls of GetHashCode().
· Prefer Query Syntax to Loops.
· Avoid Conversion Operators in Your APIs.
· Use Optional Parameters to Minimize Method Overloads.
· Understand the Attraction of Small Functions.
b. C# Language Enhancements · Augment Minimal Interface Contracts with Extension Methods
· Enhance Constructed Types with Extension Methods
· Prefer Implicitly Typed Local Variables
· Limit Type Scope by Using Anonymous Types
· Create Composable APIs for External Components
· Avoid Modifying Bound Variables
· Define Local Functions on Anonymous Types
· Never Overload Extension Methods
c. .NET Resource Management · Prefer Member Initializers to Assignment Statements.
· Use Proper Initialization for Static Class Members.
· Minimize Duplicate Initialization Logic.
· Utilize using and try/finally for Resource Cleanup.
· Avoid Creating Unnecessary Objects.
· Implement the Standard Dispose Pattern.
· Distinguish Between Value Types and Reference Types.
· Ensure That 0 Is a Valid State for Value Types.
· Prefer Immutable Atomic Value Types.
d. Expressing Designs in C# · Limit Visibility of Your Types.
· Prefer Defining and Implementing Interfaces to Inheritance.
· Understand How Interface Methods Differ from Virtual Methods.
· Express Callbacks with Delegates.
· Implement the Event Pattern for Notifications.
· Avoid Returning References to Internal Class Objects.
· Prefer Making Your Types Serializable.
· Create Large-Grain Internet Service APIs.
· Support Generic Covariance and Contravariance.
e. C# Design Practices · Create Composable APIs for Sequences.
· Decouple Iterations from Actions, Predicates, and Functions.
· Generate Sequence Items as Requested.
· Loosen Coupling by Using Function Parameters.
· Create Method Groups That Are Clear, Minimal, and Complete.
· Prefer Defining Methods to Overloading Operators.
· Understand How Events Increase Runtime Coupling Among Objects.
· Declare Only Non-Virtual Events.
· Use Exceptions to Report Method Contract Failures.
· Ensure That Properties Behave Like Data.
· Distinguish Between Inheritance and Composition.
f. Working with Generics · Use Generic Replacements of 1.x Framework API Classes.
· Define Constraints That Are Minimal and Sufficient.
· Specialize Generic Algorithms Using Runtime Type Checking.
· Use Generics to Force Compile-Time Type Inference.
· Ensure That Your Generic Classes Support Disposable Type Parameters.
· Use Delegates to Define Method Constraints on Type Parameters.
· Do Not Create Generic Specialization on Base Classes or Interfaces.
· Prefer Generic Methods Unless Type Parameters Are Instance Fields.
· Prefer Generic Tuples to Output and Ref Parameters.
· Implement Classic Interfaces in Addition to Generic Interfaces.
g. Working with LINQ · Understand How Query Expressions Map to Method Calls.
· Prefer Lazy Evaluation Queries.
· Prefer Lambda Expressions to Methods.
· Avoid Throwing Exceptions in Functions and Actions.
· Distinguish Early from Deferred Execution.
· Avoid Capturing Expensive Resources.
· Distinguish Between IEnumerable and IQueryable Data Sources.
· Use Single() and First() to Enforce Semantic Expectations on Queries.
· Prefer Storing Expression to Func.
h. Working with the Framework · Prefer Overrides to Event Handlers
· Implement Ordering Relations with IComparable and IComparer
· Avoid ICloneable.
· Use the new Modifier Only to React to Base Class Updates
· Avoid Overloading Methods Defined in Base Classes
· Learn How PLINQ Implements Parallel Algorithms
· Understand How to Use PLINQ for I/O Bound Operations
· Construct Parallel Algorithms with Exceptions in Mind
i. Dynamic Programming in C# · Understand the Pros and Cons of Dynamic.
· Use Dynamic to Leverage the Runtime Type of Generic Type Parameters.
· Use Dynamic for Parameters That Receive Anonymous Types.
· Use DynamicObject or IDynamicMetaObjectProvider for Data-Driven Dynamic Types.
· Understand How to Make Use of the Expression API.
· Use Expressions to Transform Late Binding into Early Binding.
· Minimize Dynamic Objects in Public APIs.
j. Miscellaneous · Minimize Boxing and Unboxing.
· Create Complete Application-Specific Exception Classes.
· Prefer the Strong Exception Guarantee.
· Prefer Safe Code.
· Prefer CLS-Compliant Assemblies.
· Prefer Smaller, Cohesive Assemblies.
· Minimize the Visibility of Nullable Values.
· Give Partial Classes Partial Methods for Constructors, Mutators, and Event Handlers.
· Limit Array Parameters to Params Arrays.
· Avoid Calling Virtual Functions in Constructors.
· Consider Weak References for Large Objects.
· Prefer Implicit Properties for Mutable, Nonserializab
Información
- Descripción
- Apunte sobre Best Practices C Sharp, creado por spmooney101 el 20/10/2015.
- Creado por
- spmooney101
- Visitas
- 1
- Valoración