Four basic techniques for C #

Source: Internet
Author: User
Tags foreach arrays constructor

1. If you can use interfaces to program as much as possible

. NET Framework includes classes and interfaces, which you may know are in use when writing programs. NET's class. However, in this case if you use. NET supports rather than its classes, the code becomes more stable and more usable. Please 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 a listbox from an array that can be used for any object, and this code is limited to the use of arrays only. Imagine that some time later you find that those objects exist 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 the program, you do not have to modify the program, for any implementation of the ICollection interface type it can work well:

private void LoadList (ICollection items,ListBox l)
{
  foreach (object o in items)
  l.Items.Add (o.ToString ());
}

ICollection are implemented by arrays and by collections in all system.collection. In addition, multidimensional arrays also support ICollection interfaces. If that's not enough, the database. NET class also supports ICollection interfaces. This function, written with an interface, can be used in many cases without modification.

2. Use attributes instead of raw data

Because the attribute has become an element of the language itself, it is not necessary to declare a data element larger than private. Because the code itself sees attributes as data elements, you're not losing the convenience of using simple data types. Instead, it makes your code more flexible and more powerful. Attributes make your data elements more encapsulated. property allows you to use lazy evaluation to return data. Lazy evaluation means that the value is calculated when the user requests it, rather than keeping it.

Finally, the attribute can be virtual or abstract. You can also define attributes in the interface.

There's a maintenance factor here. Note: Although the method of manipulating the two is the same, you turn a data element into an attribute, then the original client's program will not be able to access the server's new version of the program. In fact, you can use the values you want to serialize in a Web service by turning them into attributes:

private int TheMonth = 0;
[XmlAttribute ("Month")]
public int Month
{
 get {
  return TheMonth;
 }
 set {
  TheMonth = value;
 }
}

Simply passing properties can privatize all your data elements.

3. Use of delegate in the idiom of Producer/consumer

When you generate an implementation producer Idiom class, use Deletate to notify consumer. This approach is more flexible than using interfaces. Delegate is multicast, so you can support multiple users without adding additional code. This reduces the coupling between classes, as opposed to using an interface.

The following class handles the 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 producer, all they have to do is provide a specific function: Deletate.

4. Take note of initialization order

The concept of initializer is added to some variable declarations in C #. They are executed before the constructor, and the variables are actually initialized before the constructor of the base class executes.

Therefore, do not use the 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.