Another way to evaluate the Length (+ obj). Length

Source: Internet
Author: User

When binding an ASP. Net page, if the title exceeds 20, it is omitted. General Practice:
Eval ("Title") = null? "" :( Eval ("Title"). ToString (). Length <= 20? Eval ("Title"). ToString (): Eval ("Title"). ToString (). Substring (0, 17) + "... ")

 

Let me explain the results first and look at the common methods in the ASP era:

("" + Eval ("Title"). Length <= 20? ("" + Eval ("Title") :( "" + Eval ("Title"). Substring () + "... "

 

Well, my expression is a little bad. In fact, I want to demonstrate a method to avoid null and ToString, that is, "" + obj

 

Some people disagree and think this is a bad usage. In addition, if obj is not a String, there is a null exception and the performance is poor ......

 

Let's test

If obj is not a String, there is no null exception.

Let's take a look at the IL next to it.

In fact, the compiler has handled it, for example: String. Concat (obj). Note that here is a static method of the Object parameter, which is not a commonly used two parameter

 

Let's take a look at this mysterious String. Concat (Object)

Oh, after a lap, I still came back!

 

The result shows that "" + obj and obj = null? "": Obj. ToString () is exactly the same!
New Life Development Team

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.