I always assumed that DbNull.value is a single. And so you can do things like this:
VB.NET:
If someObject Is DbNull.Value Then ... End if
FROM#:
If (someObject == DbNull.Value) { ... }
But lately, I have serialized an instance of DbNull using XmlSerialiser and all of a sudden it was not superfluous. Type comparison operations (like C # (obj - DBNull)) work fine 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);
Why is this so? And how is this going? And can this happen to any other static fields?
PS: I know that the VB code sample performs a comparative comparison, and C # calls Object.Equals. Both have the same behavior with DBNull. I usually work with VB.
ligos
source share