简体   繁体   English

asp.net主属性字符串,如果字符串为空,如何在内容页面中检入

[英]asp.net master property string, how to check in content page if string is empty

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) 和内容页面中的标签,我通过执行以下操作检查字符串是否为空:if(Master.SharedInfo == null)

now my question is: why does if(Master.SharedInfo == "") not work, because the SharedInfo is a string right? 现在我的问题是:为什么if(Master.SharedInfo ==“”)不起作用,因为SharedInfo是一个字符串,对吗?

There is a handy method that "catches" both 有一种方便的方法可以“捕捉”两个

if (String.IsNullOrEmpty(Master.SharedInfo)) {
    ...
}

null and "" are not equal. null""不相等。 null means no string at all. null表示完全没有字符串。 "" is a string of length 0 . ""是长度为0的字符串。

string s = null;
int i = s.Length; // <-- Throws null reference exception

But

string s = "";
int i = s.Length; // OK, i => 0

"" and String.Empty are equivalent. ""String.Empty是等效的。 Some people state that you should always use String.Empty instead of "" , but it makes really no difference. 有人指出您应该始终使用String.Empty而不是"" ,但这实际上没有什么区别。


UPDATE UPDATE

Equal string constants are interned by the compiler, ie the compiler stores equal constants only once. 相等的字符串常量由编译器插入,即,编译器仅存储一次相等的常量。 You can make a simple test (in response to @BobTodd's comment), 您可以进行简单的测试(以回应@BobTodd的评论),

string s = ""; 
Console.WriteLine(Object.ReferenceEquals(s, "")); // --> true
Console.WriteLine(Object.ReferenceEquals(s, String.Empty)); // --> true

For the sake of completeness (according to @JoelEtherton's comment). 为了完整起见(根据@JoelEtherton的评论)。 Starting from .NET Framework 4.0 you can test 从.NET Framework 4.0开始,您可以进行测试

if (String.IsNullOrWhitespace(Master.SharedInfo)) {
    ...
}

This will catch strings like " " or "\\t" as well. 这也将捕获类似" ""\\t"字符串。

In c#, an empty string "" is not null. 在c#中,空字符串""不为null。 It's an actual string, with length equals to zero. 这是一个实际的字符串,长度等于零。

You can use string.IsNullOrEmpty(string stringToTest) to check both null and empty strings. 您可以使用string.IsNullOrEmpty(string stringToTest)来检查空字符串和空字符串。

String.Empty ("") and null are quite different String.Empty (“”)和null完全不同

It depends wholly on what has been written to Session["SharedInfo"]. 这完全取决于已写入Session [“ SharedInfo”]的内容。

If nothing has, then it will be null, otherwise its the value written, which could be an empty string or null. 如果没有任何内容,则它将为null,否则将为其写入的值,该值可以为空字符串或null。

To be sure use String.IsNullOrEmpty(string @string) 确保使用String.IsNullOrEmpty(string @string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从内容页面设置母版页的属性,并从asp.net中的另一个内容页面获取该属性值 - set property of master page from content page and get that property value from another content page in asp.net 如何替换这些“ / \\\\ []:| | &lt;&gt; + =; ,? *” asp.net中带有空字符串的字符串中的字符 - How to Replace any of these “/ \\ [ ] : | < > + = ; , ? *” Characters in a string with empty string in asp.net 从其他页面的ASP.NET母版页排除内容 - Excluding content from ASP.NET master page in other page ASP.net通过内容页面访问主页变量 - ASP.net access a master page variable through content page ASP.net母版页/内容页的简单设计问题 - ASP.net master page/content page simple design problem 将值从内容页面传递到母版页 - ASP.NET - Pass value from Content page to Master Page - ASP.NET 如何通过asp.net中的母版页显示或隐藏内容页面的左侧内容 - How to show or hide left content of content pages through master page in asp.net 如何在asp.net的内容页面的更新面板中获取母版页控件 - How to get master page control in content page's update panel in asp.net 如何在内容页面中更改主页的asp.net控件的可见性? - how to change visible of an asp.net control of master page in content page? ASP.NET WebAPI 2:如何将空字符串作为 URI 中的参数传递 - ASP.NET WebAPI 2: How to pass empty string as parameter in URI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM