1.If possible, use interfaces for programming
The. NET Framework includes classes and interfaces. When writing a program, you may know which class of. NET is being used. However, in this case, if you program with an interface supported by. NET instead of its class, the code will become more stable and the availability will be higher. Analyze the following code:
Private void LoadList (object [] items, ListBox l)
{
For (int I = 0; I <items. Length; I ++)
L. Items. Add (items [I]. ToString ());
}
This function loads ListBox from an array for any object. This Code is limited to only arrays. Imagine that you may find that the objects exist in the database or other collection. Then you need to modify the program to use different set types. If you use the ICollection interface to write that program, you don't need to modify that program. It works well for any type that implements the ICollection interface:
Private void LoadList (ICollection items, ListBox l)
{
Foreach (object o in items)
L. Items. Add (o. ToString ());
}
ICollection is implemented by arrays and collections in all System. Collections. In addition, multi-dimensional arrays also support the ICollection interface. If that is not enough, the database. NET class also supports the ICollection interface. This function written using interfaces can be used in many cases without modification.
2. Use attributes to replace original data
Because an attribute has become an element of the language itself, when declaring a data element, its scope level does not need to be greater than private. Because the Code itself regards attributes as data elements, you do not lose the convenience of using simple data types. On the contrary, it will make your code more flexible and more powerful. Attribute makes your data element more encapsulated. Attribute allows you to use lazy evaluation to return data. Lazy evaluation means that the value is calculated only when the user requests it, rather than retaining it all the time.
Finally, the attribute can be virtual or abstract. You can also define attributes in the interface.
There are also maintenance factors to note: although the two methods are the same, you change a data element into an attribute, then, the original client program will not be able to access the new version of the server. In fact, you can convert the serialized values in the Web service into attributes:
Private int TheMonth = 0;
[XmlAttribute ("Month")]
Public int Month
{
Get {
Return TheMonth;
}
Set {
TheMonth = value;
}
}
Simply using attributes, You Can privatize all your data elements.
3. Use Delegate in Idiom of Producer/Consumer
When you generate a producer idiom class, use deletate to notify consumer. This method is more flexible than the interface. Delegate is multi-point transmission, so you can support multiple users without adding additional code. Compared with interfaces, this can reduce the coupling between classes.
The following class processes keyboard input and passes it to all registered listeners:
Public class KeyboardProcessor
{
Private OnGetLine theFunc = null;
Public OnGetLine OnGetLineCallback {
Get {
Return theFunc;
}
Set {
TheFunc = value;
}
}
Public void Run (){
String s;
Do {
S = Console. ReadLine ();
If (s. Length = 0)
Break;
If (theFunc! = Null ){
System. Delegate [] funcs = theFunc. GetInvocationList ();
Foreach (OnGetLine f in funcs ){
Try {
F (s );
} Catch (Exception e ){
Console. WriteLine
("Caught Exception: {0}", e. Message );
}
}
}
} While (true );
}
Any number of listeners can be registered to the producer. All they need to do is provide a specific function: deletate.
4. Pay attention to the initialization sequence
The initializer concept is added to some variable declarations in C. They are executed before the constructor. In fact, variables are initialized before the constructor of the base class is executed.
Therefore, do not use data in the base class when initializing variables, because they are not yet constructed.