我应该使用空字符串,为什么? [英] Which should I use for empty string and why?

查看:143
本文介绍了我应该使用空字符串,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and">What是的String.Empty和&ldquo之间的差异;&rdquo;的

我应使用分配一个空字符串及以下的,为什么?

 字符串VARIABLENAME =;
 

 字符串VARIABLENAME =的String.Empty;
 

解决方案

的String.Empty 是通常建议。

为什么呢?因为它传达的意思,你很明确地说,你希望字符串是空的。

此外,创建一个字符串对象(此对象将会在应用程序中重用,因为在.NET中字符串的实习),的String.Empty 不创建一个新的对象。请参见这里

Possible Duplicate:
What is the difference between String.Empty and “”

Which of the following should I use for assigning an empty string and why?

string variableName = "";

or

string variableName = string.Empty;

解决方案

String.Empty is what is normally recommended.

Why? Because it conveys meaning, you are very explicitly saying that you want the string to be empty.

Additionally, "" creates a string object (this object will be reused across the applications, because strings in .NET are interned), String.Empty does not create a new object. See here.

这篇关于我应该使用空字符串,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆