2 * Prefer String.Compare to changing case
4 If you need to compare strings ignoring case, you should use
5 String.Compare rather than calling ToLower on both. This
8 * Prefer ToLower to ToUpper
10 Apparently, ToLower is faster.
12 * Use the InvariantCulture
14 InvariantCulture should be used for all "non-linguistic
15 identifiers" (see http://tinyurl.com/9vqus -- the 2.0 string
16 recommendations). It is faster and more correct.
18 Some methods in the string class that you might not think are
19 culture sensitive really are. The following methods are
27 - .IndexOf (string, ...)
28 - .LastIndexOf (string, ...)
30 The methods in System.Web.Util.StrUtils exist to make correct
31 calls less verbose. They use the InvariantCulture.
35 In controls, it is important to keep the size of controls
36 small. Controls can be replicated many times on a page due to
37 data bound controls. The more memory each control allocates,
38 the more GCs we have to go through.
42 There are a few techniques to save space in the number of fields.
44 * Use [Flags] enums rather than many bool's
46 Each bool takes up 1 byte. If you use a flags enum you can
47 pack 8 bools into the same amount of space.
49 * Use the Events framework
53 public event EventHandler x;
55 it creates a field. Most of these fields never get
56 used. Control has a property called Events which holds a
57 linked list of events that get created. Thus, events only take
58 up space when one uses them. An example of using this:
60 static object event_name_blah = new object ();
62 public event EventType EventName {
63 add { Events.AddHandler (event_name_blah, value); }
64 remove { Events.RemoveHandler (event_name_blah, value); }
68 If your control has a OnEventName that invokes EventName, you
71 EventType deleg = (EventType) Events [event_name_blah];
77 Keep the view state small.
79 Remember that whatever gets stored in ViewState after tracking
80 starts needs to be sent over the wire.
82 Store in ViewState the minimum amount of objects needed to
85 * Store optimized classes.
87 It is important to store things in terms of primitive types
88 (int, short, bool, byte, string), Hashtables, ArrayLists,
89 object arrays, and specially optimized types (Unit, Color,
90 Pair, Triplet) when saving viewstate. Otherwise, things will
91 have to be put in a more expensive format over the wire.
93 * Store int values rather than enums.
95 If you store an enum, the fully qualified name to the enum
96 needs to be sent over the wire. Cast the enum value to an
97 integer when storing it in view state. Keep in mind that
100 MyEnum e = (MyEnum) o;
102 Works, so you don't need any complex code when getting the
105 * Return null in SaveViewState when possible
107 For example, if you normally save the state of your 3 children
108 to a triplet, but all 3 values are null, return null, rather
109 than new Triplet (null, null, null);
111 Often, doing this allows the framework to avoid saving
112 anything about many layers of controls in the viewstate.
116 Accessing a property in the controls usually means reading
117 from ViewState. Try assigning property values to local
118 variables if they are going to be used more than once and are
119 known to not change while a given method runs.
121 * Handle Enabled and Visible.
123 If Enabled is false, your control should not handle any
124 postback. If Visible is false, your control does not render
125 anything (but still keeps its state). Usually the parent takes