0

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.

10 Answers 10

15

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

Sign up to request clarification or add additional context in comments.

3 Comments

Why does the compiler not compile 'myList[0].Items = null'? Is myList readonly?
Having thought about it for another minute or so, I guess the answer to my own question is that the problem is that the C# compiler won't automatically convert an object to a bool. As an old C/C++ hand I keep forgetting how different C# is!
@AAT it will complain that it can't convert it to a bool. myList[0].Items = null on its own will compile.
5

= 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.

Comments

3

=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.

Comments

3

'=' is for assignment, not for comparing. Use '=='

Comments

2
if (myList[0].Items == null){Console.WriteLine("null!");}

Comments

2

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.

Comments

1

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

Comments

1

= is the assignment operator, you should use the equality operator ==

Comments

1

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

Comments

1
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.