C # Summary of design skills

Source: Internet
Author: User
C # Summary of design skills
As a programmer, how can we improve our programming level from the details? There should be no reason not to grasp the following points:
1. Coding habits
Coding habits are very important for a team to develop. At least we need to follow the naming rules below: Class Name: the first letter of each word in uppercase eg: myclass field: the first letter of the first word is lower case eg: myfiled interface: start with I; Use meaningful variables and namespaces, for example, when defining an exception class, we generally use exception as the suffix, and methods with return values must start with get. All member variables should be declared at the top, at the same time, a blank line is used to separate them from attributes and Methods. Braces are always placed on a new line.
2. Delegate
For Junior programmers, delegation and events are indeed difficult for many people, including those I did not understand when I first entered the company. Haha, I just blame myself for not learning well when I was at school. Don't hit me. To understand the delegation, we can start from the following aspects: First, we can think of it as a function pointer in C ++ (needless to say, that university has opened C/C ++ courses, but the difference is that delegation is completely object-oriented (both encapsulation method and encapsulation instance). How can this problem be understood? For example, there is a method where many classes need to be used and implemented. There are two methods that can be completed. At first, the method is written into a class and then abstracted into a base class, the problem is that C # only supports single inheritance, which sometimes leads to waste of method calls, for example, there are methods a, B in the base class, c, but maybe I have a subclass that does not require the three methods A, B, and C. At this time, the delegate can completely avoid such problems. For example, the boss has a lot to do every day. In this case, to relieve his burden, he may consider finding an agent (Assistant ), one day, he told his assistant that I needed to fly from Chengdu to Beijing. Then his assistant would say, "OK, I have booked your ticket.
3. Use of equals ()
It is used to determine whether two objects point to the same object. Sometimes this function cannot meet our needs. For example, if two objects have the same name, we think the objects are equal. In this case, you can only customize the equals method:
Public CLAS user
{
Public String firstname;
Public String lastname;
Public override bool equals (Object OBJ)
{
If (OBJ = NULL) return false;
If (obj. GetType (). Equals (this. GetType () = false) return false;
User Other;
Other = (User) OBJ;
Return this. firstname. Equals (other. firstname) & this. lastname. Equals (other. lastname );
}
Public override int gethashcode ()
{
Return this. firstname. gethashcode () + this. lastname. gethashcode ();
}
}
PS: If equals is rewritten, gethashcode must also be rewritten. Otherwise, a warning will be issued during program compilation!
Hashtable provides efficient search. It is based on (key/value) and is unique.
4. idisposable
The purpose is to clear objects. Generally, we turn it on and close it after use. Sometimes we need to implement the idisposable interface to achieve more advanced requirements.
Public class myclass: disposable
{
Public void dispose ()
{
OBJ. Close ();
}
}
When using this, consider four points: release all uncontrolled resources; release all controlled resources; set a flag to indicate that the objects we have released have been processed. In this case, you need to check the status in a public method. In case the object is released but activated, terminate the failnes method.
5. icloneable
Cloning is actually copying objects.
User obj1 = new user ("Kim", "Luo", 1000.0 m );
User obj2;
Obj2 = obj1.clone ();
Shallow light replication: only copying top-level objects
Public class user: icloneable
{
Public object clone ()
{
Return new user (this. firstname, this. lastname, this. Balance );
}
}
Equivalent
Public class user: icloneable
{
Public object clone ()
{
Return this. memberwiseclone ();
}
}
Deep replication: copying objects and sub-objects
6. String-related
String is an unchangeable object. The string connection operation "+" does not change the current string, but creates and returns a new string, which is slow. However, the stringbuilder class is different. We mainly use it for string connection operations, fast.
7. String resident
String str1 = "ABC ";
String str2 = "ABC ";
In fact, str1 and str2 share the same memory address in the memory instead of occupying a memory address. This is because of the hash table. When you generate a new string, it first finds whether there is a corresponding string in the system memory. If there is a new string, it will not allocate a new space. To save memory space.
But this is different:
String str1 = "ABC ";
String str2 = "";
String str3 = "BC ";
String str4 = str2 + str3;
Although the output of str1 and str4 are both ABC, they do not share the same memory in the system. However, to achieve this effect
Is to make them share the same piece of memory. You can call these two methods: public static intern (string Str) and public static isintern (string Str.

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.