string.Empty与"",null的区别,用哪个更好呢?
from:http://www.30wan.com/articleview/2008-5-7/article_view_32502.htm
s==“”
s==string.Empty
s.Equals(”")
s.Equals(string.Empty)
s.Length==0
#region测速代码
intcnTries=10;
intcnIterations=1000000000;
=0,fShortQuotes=0,fLongQuotes=0;
=0,fShortEmpty=0,fLongEmpty=0;
=0,fShortDotQuotes=0,fLongDotQuotes=0;
=0,fShortDotEmpty=0,fLongDotEmpty=0;
=0,fShortDotLength=0,fLongDotLength=0;
=string.Empty;
=“Thisisashortstringtotestemptystringcomparison”;
=“Thisisalongstringtotesttheefficiencyofcomparingwithemptystrings,whichmeansithastobelike,reallylong.AndI’mstartingtorunoutofuselessthingstosay…”;
=0;j<cnTries; j)
{
DateTime.Now;
=0;i<cnIterations; i)
{
==“”);
-dtStart;
=0;i<cnIterations; i)
{
==“”);
-dtStart;
=0;i<cnIterations; i)
{
==“”);
-dtStart;
DateTime.Now;
=0;i<cnIterations; i)
{
==string.Empty);
-dtStart;
=0;i<cnIterations; i)
{
==string.Empty);
-dtStart;
=0;i<cnIterations; i)
{
==string.Empty);
-dtStart;
DateTime.Now;
=0;i<cnIterations; i)
{
-dtStart;
=0;i<cnIterations; i)
{
-dtStart;
=0;i<cnIterations; i)
{
-dtStart;
DateTime.Now;
=0;i<cnIterations; i)
{
string.Empty));
-dtStart;
=0;i<cnIterations; i)
{
string.Empty));
-dtStart;
=0;i<cnIterations; i)
{
string.Empty));
-dtStart;
DateTime.Now;
=0;i<
null是C#要害字,是表示不引用任何对象的空引用的文字值。null是引用类型变量的默认值。那么也只有引用型的变量可以为NULL,假如inti=null,的话,是不可以的,因为Int是值类型的。
string.Empty就相当于"",但他俩和NULL是两码事。
据说是:string.Empty不分配存储空间
""分配一个长度为空的存储空间所以string.Empty比""效率要高点。(效率高是对的,但是到底是否分配存储空间,还不明确,待核实)
一个外国人做了一个测试,测试的对象有5种,分别是:
通过下列的测试代码(如要看请点击下面的加号),判定谁的效率更高~