if i have a master property string for example:
public String SharedInfo
{
get { return (String)Session["SharedInfo"]; }
set { Session["SharedInfo"] = value; }
}
and a label in an content page, i check if the string is empty by doing:
if(Master.SharedInfo == null)
now my question is: why does if(Master.SharedInfo == “”) not work, because the SharedInfo is a string right?
There is a handy method that “catches” both
nulland""are not equal.nullmeans no string at all.""is a string of length0.But
""andString.Emptyare equivalent. Some people state that you should always useString.Emptyinstead of"", but it makes really no difference.UPDATE
Equal string constants are interned by the compiler, i.e. the compiler stores equal constants only once. You can make a simple test (in response to @BobTodd’s comment),
For the sake of completeness (according to @JoelEtherton’s comment). Starting from .NET Framework 4.0 you can test
This will catch strings like
" "or"\t"as well.