Best Practices in
framework 2.0
1)
The
runtime optimizes the performance of 32-bit integer types (Int32 and UInt32),
so use those types for counters and other frequently accessed integral
variables.
2)
For
floating-point operations, Double is the most efficient type because those
operations are optimized by hardware.
3)
Garbage
collection occurs only when needed or when triggered by a call to GC.Collect.
Automatic garbage collection is optimized for applications where most instances
are short-lived, except for those allocated at the beginning of the
application. Following that design pattern will result in the best performance.
string s;
s =
"wombat"; // "wombat"
s += "
kangaroo"; // "wombat kangaroo"
s += "
wallaby"; // "wombat kangaroo wallaby"
s += "
koala"; // "wombat kangaroo wallaby koala"
Console.WriteLine(s);
Only the
last string has a reference; the other three will be disposed of during garbage
collection. Avoiding these types of temporary strings helps avoid unnecessary
garbage collection, which improves performance. There are several ways to avoid
temporary strings:
BEST PRACTICES Boxing
and unboxing
Boxing and unboxing incur overhead, so
you should avoid them when programming intensely repetitive tasks. Boxing also
occurs when you call virtual methods that a structure inherits from System.Object,
such as ToString.
Follow these tips to avoid unnecessary
boxing:
4)
Implement
type-specific versions (overloads) for procedures that accept various value
types. It is better practice to create several overloaded procedures than one
that accepts an Object argument.
5)
Use
generics whenever possible instead of accepting Object arguments.
6)
Override
the ToString, Equals, and GetHash virtual members when defining structures.
Regular Expression
7)
When
validating input, always begin regular expressions with a “^” character and end
them with “$”. This system ensures that input exactly matches the specified
regular expression and does not merely contain matching input.
No comments:
Post a Comment