Four basic skills of C #

xiaoxiao2021-03-06  74

1. If you might try to use the interface to program the .NET framework includes classes and interfaces, when writing programs, you may know which class is using .NET. However, in this case if you use the .NET supported interface instead of its class to program, the code will be more stable, usability will be higher. Please analyze the following code:

Private void loadingList (objectbox L) {for (int i = 0; i

This function is loaded from one of an array that can be any object, which is limited to only an array. Imagine that you find those objects in the database, or 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 the program, and you can work well for any type of ICollection interface:

Private Void LoadList (iCollection Items, ListBox L) {Foreach (Object O IN Items) L.Items.Add (O.ToString ());

ICollection is implemented in array and collection of all system.collections. In addition, the multidimensional array also supports the iCollection interface. If it is not enough, the database .NET class also supports the iCollection interface. This function written with the interface does not need to be changed, can be used in many cases.

2. Use properties instead of raw data

Because the attribute has become the element of the language itself, its scope level is not necessary to be greater than private when it has become the element of the language itself. Because the code itself looks at the property as a data element, you don't lose the convenience of using the simple data type. On the contrary, it will make your code more flexible and more powerful. The property makes your data element packaging better. Properties allow you to use Lazy Evaluation to return data. Lazy Evaluation means calculating its value when the user requests, not always keeping it.

Finally, the attribute can be Virtual or Abstract. You can also define properties in the interface.

There is also a maintenance factor should note: Although the method of operation is the same, you turn a data element into an attribute, then the original client's program cannot access the new version of the server. In fact, for the value you want to implement in Web Service, you can use them into properties:

Private int called = 0; [XMlattribute ("MONTH")] public int month {get {returnTh = value;}}

Simply passing attributes to make your all data elements privatize.

3. Using Delegate in PRODUCER / Consumer IDiom

When you generate an implementation of the Producter IDiom class, use Deletate to notify the consumer. This method is more flexible relative to the interface. Delegate is multi-point transmission, so you don't have to add an additional code you will support multi-user. The coupling between the classes can be lowered relative to the interface.

The following class handles the keyboard input and pass 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 = console.readline (); if (s.Length == 0) Break; if (thefunc! = null) {system.delegate [] funcs = thefuncate [] funcs = thefunc.getinvocationList (); foreach (ONGETLINE FIN FUNCS) ) {Try {f (s);} catch (Exception E) {Console.Writeline ("Caught Exception: {0}", E.MESSAGE);}}}}}}}} Register to Producter, what they have to do is just provide a specific function: deletate.

4. Pay attention to the initialization order

C # is added to some variable declarations to the concept of Initializer. They are executed before the constructor, and the actual variable is initialized before the constructor of the base class is executed.

Therefore, do not use the data in the base class when the variable is initialized, because they have not been constructed.

转载请注明原文地址:https://www.9cbs.com/read-109100.html

New Post(0)