. Net using,string. On empty

Source: Internet
Author: User

The first two days enough OH year company training, said the coding optimization. Now the preliminary summary: (some are not sure to talk about it)

  1. Multiple string concatenation (especially in the loop), it is advisable to use the Stringbuilder.append () method, less string +, as for string. Format () is more resource-less. Of course, if the string concatenation is small, within 3 times, that also casually, in order to beautiful string. Format () is also available.
  2. String. The essence of Empty is "", reflector decompile string.    Empty result: public static readonly string Empty = ""; So string. Empty and "" can be considered the same.
  3. Equals () and = = Do not. "All classes in Java are inherited from the base class of object, and a method of equals is defined in the base class of object, and the initial behavior of this method is to compare the memory address of the object, but in some class libraries This method is overwritten , such as String, Integer,date in these classes equals has its own implementation, and is no longer a comparison class in the heap memory of the storage address. " So, for the int equivalent type and string, Equals () is no different than = =. For reference types, use Equals () as much as possible, especially if you implement equals () yourself.
  4. The nature of the using () is try{}, finally{}, and the compiler writes close (), Dispose (), and so on in finally{} to release resources. The following code:
    1. string s = String. Empty;using (StreamReader sr =new StreamReader (@ "C:\\a.txt")) {s = Sr. ReadToEnd (); }console.writeline (s);
    2. Anti-compile IL results:
    3. So there is no need to write close (), Dispose (). using{} Similar to syntax sugar, which is a mixture of code in using{}.
    4. Another mention, in try{return xx;} catch{return xx;} A return in finally{} will appear in a strange order of execution, not necessarily returned in try{}, and may have to be executed finally{}. Therefore, it is more safe and clear that we should return to the end after the unification of finally{}.

. Net using,string. On empty

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.