I've always assumed that DbNull.value was a singleton. And thus you could do things like this:
VB.NET:
If someObject Is DbNull.Value Then
...
End if
C#:
If (someObject == DbNull.Value)
{
...
}
But recently, I serialised a DbNull instance using the XmlSerialiser and suddenly it wasn't a singleton any more. Type comparison operations (like C#'s (obj is DBNull)) work OK though.
Code follows:
[Serializable, System.Xml.Serialization.XmlInclude(typeof(DBNull))]
public class SerialiseMe
{
public SerialiseMe() { }
public SerialiseMe(object value)
{
this.ICanBeDbNull = value;
}
public Object ICanBeDbNull { get; set; }
}
public void Foo()
{
var serialiseDbNull = new SerialiseMe(DBNull.Value);
var serialiser = new System.Xml.Serialization.XmlSerializer(typeof(SerialiseMe));
var ms = new System.IO.MemoryStream();
serialiser.Serialize(ms, serialiseDbNull);
ms.Seek(0, System.IO.SeekOrigin.Begin);
var deSerialisedDbNull = (SerialiseMe)serialiser.Deserialize(ms);
// Is false, WTF!
var equalsDbNullDeserialised = deSerialisedDbNull.ICanBeDbNull == DBNull.Value;
// Is false, WTF!
var refEqualsDbNullDeserialised = object.ReferenceEquals(deSerialisedDbNull.ICanBeDbNull, DBNull.Value);
// Is true.
var convertIsDbNullDeserialised = Convert.IsDBNull(deSerialisedDbNull.ICanBeDbNull);
// Is true.
var isIsDbNullDeserialised = deSerialisedDbNull.ICanBeDbNull is DBNull;
}
Why is this the case? And how does it happen? And can it possibly happen with any other static fields?
PS: I am aware the VB code sample is doing a reference comparison and c# is calling Object.Equals. Both have the same behaviour with DBNull. I usually work with VB.