1. If possible, try to use interfaces to program
The .NET framework includes classes and interfaces. When writing programs, 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 classes, the code will become more stable and more usable. Please analyze the following code:
private void LoadList (object [] items, ListBox l)
{
for (int i = 0; i < ;i++)
(items[i].ToString ());
}
This function loads a ListBox from an array that can be used for any object, and this code is limited to only use arrays. Imagine that after some time you find that those objects are in the database or in other collections. Then you need to modify the program to use different collection types. If you use the ICollection interface to write that program, you don't have 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)
( ());
}
ICollection is implemented by arrays and collections in all. In addition, multi-dimensional arrays also support the ICollection interface. If that's not enough, the database .NET class also supports the ICollection interface. This function written on the interface can be used in many cases without needing to be modified.
2. Use attributes instead of original data
Because attributes have become elements of the language itself, it is not necessary that their scope level is greater than private when declaring a data element. Because the code itself treats attributes as data elements, you do not lose the convenience of using simple data types. Instead, it will make your code more flexible and powerful. Attributes make your data elements more encapsulated. Properties allow you to use lazy evaluation to return data. lazy evaluation means to calculate its value only when the user requests it, rather than keeping it all the time.
Finally, the property can be either virtual or abstract. You can also define properties in the interface.
There are also maintenance factors here. It should be noted that although the operation method is the same, if you turn a data element into a property, then the original client's program cannot access the new version of the program on the server. In fact, for the values you want to implement serialization in the Web service, you can turn them into properties to use:
private int TheMonth = 0;
[XmlAttribute ("Month")]
public int Month
{
get {
return TheMonth;
}
set {
TheMonth = value;
}
}
Simple attributes can make all your data elements private.
3. Use Delegate in the Idiom of Producer/Consumer
When you generate a producer iom class, use delete to notify the consumer. This method is more flexible than using interfaces. Delegate is multicast, so you can support multiple users without adding extra code. Doing so with an interface can reduce 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 (){
// Read input.
// If there is any listeners, publish:
string s;
do {
s = ();
if ( == 0)
break;
if (theFunc != null){
[] funcs =();
foreach (OnGetLine f in funcs) {
try {
f (s);
} catch (Exception e) {
("Caught Exception: {0}", );
}
}
}
} while (true);
}
Any number of listeners can be registered with the producer, all they have to do is provide a specific function: delete.
4. Pay attention to the initialization order
The concept of initializer is added to some variable declarations in C#. They are executed before the constructor, and actually the variables are initialized before the base class's constructor is executed.
Therefore, do not use data from the base class when initializing variables, because they have not been constructed yet.
The .NET framework includes classes and interfaces. When writing programs, 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 classes, the code will become more stable and more usable. Please analyze the following code:
private void LoadList (object [] items, ListBox l)
{
for (int i = 0; i < ;i++)
(items[i].ToString ());
}
This function loads a ListBox from an array that can be used for any object, and this code is limited to only use arrays. Imagine that after some time you find that those objects are in the database or in other collections. Then you need to modify the program to use different collection types. If you use the ICollection interface to write that program, you don't have 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)
( ());
}
ICollection is implemented by arrays and collections in all. In addition, multi-dimensional arrays also support the ICollection interface. If that's not enough, the database .NET class also supports the ICollection interface. This function written on the interface can be used in many cases without needing to be modified.
2. Use attributes instead of original data
Because attributes have become elements of the language itself, it is not necessary that their scope level is greater than private when declaring a data element. Because the code itself treats attributes as data elements, you do not lose the convenience of using simple data types. Instead, it will make your code more flexible and powerful. Attributes make your data elements more encapsulated. Properties allow you to use lazy evaluation to return data. lazy evaluation means to calculate its value only when the user requests it, rather than keeping it all the time.
Finally, the property can be either virtual or abstract. You can also define properties in the interface.
There are also maintenance factors here. It should be noted that although the operation method is the same, if you turn a data element into a property, then the original client's program cannot access the new version of the program on the server. In fact, for the values you want to implement serialization in the Web service, you can turn them into properties to use:
private int TheMonth = 0;
[XmlAttribute ("Month")]
public int Month
{
get {
return TheMonth;
}
set {
TheMonth = value;
}
}
Simple attributes can make all your data elements private.
3. Use Delegate in the Idiom of Producer/Consumer
When you generate a producer iom class, use delete to notify the consumer. This method is more flexible than using interfaces. Delegate is multicast, so you can support multiple users without adding extra code. Doing so with an interface can reduce 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 (){
// Read input.
// If there is any listeners, publish:
string s;
do {
s = ();
if ( == 0)
break;
if (theFunc != null){
[] funcs =();
foreach (OnGetLine f in funcs) {
try {
f (s);
} catch (Exception e) {
("Caught Exception: {0}", );
}
}
}
} while (true);
}
Any number of listeners can be registered with the producer, all they have to do is provide a specific function: delete.
4. Pay attention to the initialization order
The concept of initializer is added to some variable declarations in C#. They are executed before the constructor, and actually the variables are initialized before the base class's constructor is executed.
Therefore, do not use data from the base class when initializing variables, because they have not been constructed yet.