What does it mean when it says 'int' is never equal to 'null' of type 'int?'

c#

Solution

The type of `CreatedBy` is `int` which is a type that cannot take the value `null`.

From what you currently have, it is not possible to detect whether or not `CreatedBy` has been set. Suppose that its default value is `0`. Then the value is set to `1`, and then back to `0`. Now, how can you distinguish the current `0` from the original unmodified `0`?

If what you want to do is detect whether or not the value is `0`, well I don't think I need to tell you how to do that. If you really want to detect whether or not the value has ever been set you'll need to maintain a `bool` flag and set that flag the first time the property's setter executes.

Problem

I am trying to compare the following: ``` if (e.CreatedBy == null) ``` But this is giving me an error saying: The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int?' Can someone help tell me how I can check if the value of CreatedBy has not been set yet? Note here's the definition of CreatedBy: ``` public int CreatedBy { get; set; } ```

Original source

Related problems