[英]C# Strings - Why does null give me a different result as opposed to ""?
I have 2 forms set up.我设置了 2 forms。 In the first form I have the following code.
在第一种形式中,我有以下代码。
frm_BL addBranch = new frm_BL();
do
{
addBranch.ShowDialog();
if (addBranch.txtAmount.Text == "")
{
break;
}
} while (true);
In the main form.在主窗体中。 And just this in the second form.
而这只是第二种形式。
private void btnAccept_Click(object sender, EventArgs e)
{
this.Close();
}
However I found that if I change the code of the main form to:但是我发现如果我将主窗体的代码更改为:
if (addBranch.txtAmount.Text == null) //changed to null
The second form keeps popping up.第二种形式不断出现。 But if it stays at
但如果它停留在
if (addBranch.txtAmount.Text == "")
It closes the form.它关闭表单。 Can someone explain why that is?
有人可以解释这是为什么吗?
null and empty string are two different things, if you want to handle both cases you can use String.IsNullOrEmpty instead null 和空字符串是两种不同的东西,如果你想处理这两种情况,你可以使用 String.IsNullOrEmpty 代替
null means the reference to the string you have does not exist (you point to nothing) null 表示对您所拥有的字符串的引用不存在(您没有指向任何内容)
empty string means you have a reference to a string that contains nothing (pointer to an empty array of characters for example).空字符串意味着您引用了一个不包含任何内容的字符串(例如,指向一个空字符数组的指针)。
The best way to do this is:最好的方法是:
if (String.IsNullOrEmpty(addBranch.txtAmount.Text))
The txtAmount.Text
property is a string
containing the content of the textbox. txtAmount.Text
属性是一个包含文本框内容的string
。 If the textbox is empty then it's a zero-length string.如果文本框为空,则它是一个零长度字符串。
Checking for equality with null
is saying "If the textbox doesn't have a string...", which will always be false.检查与
null
是否相等表示“如果文本框没有字符串...”,这将始终为假。 The correct condition to check is "If the textbox's string is empty...".要检查的正确条件是“如果文本框的字符串为空...”。
Using the IsNullOrEmpty
method checks for both conditions.使用
IsNullOrEmpty
方法检查这两个条件。 In this case the string should never be null, but it doesn't hurt to check.在这种情况下,字符串永远不应是 null,但检查一下也无妨。
Note that ""
is an empty string (equivalent to String.Empty
), whereas null
says the string doesn't exist.请注意,
""
是一个空字符串(相当于String.Empty
),而null
表示该字符串不存在。
A null
String
is different than an empty String
. null
String
与空String
不同。 Use String.Empty()
instead.请改用
String.Empty()
。
I'd look at the definitions for more info:我会查看定义以获取更多信息:
The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables.
null 关键字是表示 null 引用的文字,它不引用任何 object。null 是引用类型变量的默认值。 Ordinary value types cannot be null. However, C# 2.0 introduced nullable value types.
普通的值类型不能是null。但是,C# 2.0 引入了可空值类型。
http://msdn.microsoft.com/en-us/library/edakx9da.aspx http://msdn.microsoft.com/en-us/library/edakx9da.aspx
The value of this field (String.Empty) is the zero-length string, "".
此字段 (String.Empty) 的值是零长度字符串“”。 In application code, this field is most commonly used in assignments to initialize a string variable to an empty string.
在应用程序代码中,此字段最常用于赋值以将字符串变量初始化为空字符串。
http://msdn.microsoft.com/en-us/library/system.string.empty.aspx http://msdn.microsoft.com/en-us/library/system.string.empty.aspx
One more thing that we see there is that "to test whether the value of a string is either Nothing or String.Empty, use the IsNullOrEmpty method."我们在那里看到的另一件事是“要测试字符串的值是 Nothing 还是 String.Empty,请使用 IsNullOrEmpty 方法。”
So when something is null
it represents a reference to nothing (most commonly it references 0), whereas when a string contains a null value this means that the string is empty but it holds a reference to valid memory.因此,当某物为
null
时,它表示对任何内容的引用(最常见的是它引用 0),而当字符串包含 null 值时,这意味着该字符串为空,但它包含对有效 memory 的引用。
txtAmount.Text will never return a null whatsoever you write a code(in c# and for every valid (non null)TextBox). txtAmount.Text 永远不会返回 null 无论你写什么代码(在 c# 和每个有效的(非空)TextBox)。
TextBox.Text Returns a string which is either Empty string or non empty string. TextBox.Text 返回一个字符串,它是空字符串或非空字符串。
In addition to the already correct answers I would also add Trim() to the check because in most of the cases a string only with blanks is not something that is accepted especially for a TextBox input.除了已经正确的答案之外,我还会将 Trim() 添加到检查中,因为在大多数情况下,仅包含空格的字符串不被接受,尤其是对于 TextBox 输入。
if (string.IsNullOrEmpty(addBranch.txtAmount.Text.Trim()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.