简体   繁体   中英

!= 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?

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:

http://msdn.microsoft.com/en-us/library/6a71f45d(v=vs.80).aspx

= null is assignment . You should use == null

You assign the null value to myList[0].Items and tries to use it as bool in if statement. 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.

=null you set the value to null

!= null you check if it is different from null

If you want to compare if it is equal to null, use == null instead of = 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. You probably mean 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. For reference types other than string, == returns true if its two operands refer to the same object. 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

= null is an assignment. == null is a condition.

if (myList[0].Items != null)

is a negative comparison . It checks if myList[0].Items is not equal to 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).

=是赋值运算符,应使用等于运算符==

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM