[英]!= null vs. =null
See below code first please. 请首先参见下面的代码。
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
public struct MyStruct
{
public List<MyStructItem> Items;
}
public struct MyStructItem
{
public string Value;
}
static void Main(string[] args)
{
List<MyStruct> myList = new List<MyStruct>();
myList.Add(new MyStruct());
//(!) it haven't comipled.
if (myList[0].Items = null){Console.WriteLine("null!");}
//(!) but it have compiled.
if (myList[0].Items != null) { Console.WriteLine("not null!"); }
}
}
}
What is difference between !=null
and =null
in that case? 在这种情况下,
!=null
和=null
什么区别?
thanks. 谢谢。
You are using the assignment operator =
instead of the equality operator ==
. 您正在使用赋值运算符
=
而不是等于运算符==
。
Try: 尝试:
//(!) it haven't comipled.
if (myList[0].Items == null){Console.WriteLine("null!");}
//(!) but it have compiled.
if (myList[0].Items != null) { Console.WriteLine("not null!"); }
The difference is one compiles and one doesn't :-) 区别在于一个编译而一个不编译:-)
C# Operators: C#运算符:
http://msdn.microsoft.com/en-us/library/6a71f45d(v=vs.80).aspx http://msdn.microsoft.com/zh-CN/library/6a71f45d(v=vs.80).aspx
= null
is assignment . = null
为赋值 。 You should use == null
您应该使用
== null
You assign the null
value to myList[0].Items
and tries to use it as bool in if
statement. 您将
null
值分配给myList[0].Items
并尝试在if
语句中将其用作布尔值。 That is why code can not be compiled. 这就是为什么代码无法编译的原因。
For example, this code compiles successfully: 例如,此代码成功编译:
bool b;
if (b = true)
{
...
}
Because you set true
value to b
and then check it in if
statement. 因为您将
true
设置为b
,然后在if
语句中进行检查。
=null
you set the value to null =null
,将值设置为null
!= null
you check if it is different from null != null
,检查是否与null不同
If you want to compare if it is equal to null, use == null
instead of = null. 如果要比较是否等于null,请
use == null
而不是= null。
'=' is for assignment, not for comparing. “ =”用于分配,不用于比较。 Use '=='
使用'=='
if (myList[0].Items == null){Console.WriteLine("null!");}
First off, myList[0].Items = null
will set the object to null. 首先,
myList[0].Items = null
会将对象设置为null。 You probably mean myList[0].Items == null
您可能是说
myList[0].Items == null
And regarding the difference, == checked if something is equal. 关于差异,==检查是否相等。 != checks if something is not equal.
!=检查是否不相等。
For predefined value types, the equality operator (
==
) returns true if the values of its operands are equal, false otherwise.对于预定义的值类型,等于运算符(
==
)如果其操作数的值相等,则返回true,否则返回false。 For reference types other than string,==
returns true if its two operands refer to the same object.对于字符串以外的引用类型,如果
==
的两个操作数引用同一对象,则==
返回true。 For the string type,==
compares the values of the strings.对于字符串类型,
==
比较字符串的值。
And 和
The assignment operator (
=
) stores the value of its right-hand operand in the storage location, property, or indexer denoted by its left-hand operand and returns the value as its result.赋值运算符(
=
)将其右侧操作数的值存储在其左侧操作数表示的存储位置,属性或索引器中,并返回该值作为其结果。 The operands must be of the same type (or the right-hand operand must be implicitly convertible to the type of the left-hand operand).操作数必须具有相同的类型(或右侧操作数必须隐式转换为左侧操作数的类型)。
Reference: http://msdn.microsoft.com/en-us/library/6a71f45d(v=vs.71).aspx 参考: http : //msdn.microsoft.com/en-us/library/6a71f45d(v=vs.71).aspx
= null
is an assignment. = null
是分配。 == null
is a condition. == null
是一个条件。
if (myList[0].Items != null)
is a negative comparison . 是否定的比较 。 It checks if
myList[0].Items
is not equal to null
. 它检查
myList[0].Items
是否不等于null
。
if (myList[0].Items = null)
is an assignment. 是一项任务。 It would normally assign
null
to myList[0].Items
and return true (in languages like C++), however, in C#, this won't compile (because of this common error). 通常,它将为
myList[0].Items
分配null
并返回true(在类似C ++的语言中),但是,在C#中,它将不会编译(由于此常见错误)。
=
是赋值运算符,应使用等于运算符==
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.