人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

May 15, 2006

Is string null or empty?

Filed under: Code snippets

Cheap and interesting trick:

    Convert.ToString((object)stringVar) == ""
 
This works because Convert.ToString(object) returns an empty string if object is null. 

注意!!!
Convert.ToString(string) returns null if string is null.

(Or, if you’re using .NET 2.0 you could always use String.IsNullOrEmpty.)

其实以上的代码还可以再优化一点点:

根据FxCop的建议,我们应该使用

 Convert.ToString((object)stringVar).Length==0

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/05/15/is-string-null-or-empty/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham