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