C # programming advice

Source: Internet
Author: User

1. Use interfaces for programming as much as possible

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:

     LoadList (      ( i = ; i < items.Length;i++    } 

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:

           ( o     }  

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:

   TheMonth =         [XmlAttribute (                        TheMonth =    } 


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.
Http://www.cnblogs.com/roucheng
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 = 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.