-
Type: Improvement
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: 1.8
-
Component/s: None
-
None
The BsonDocument object model has a tricky way of ignoring C# null values sometimes, which allows constructs like the following:
BsonValue a = 1; BsonValue b = null; var document = new BsonDocument { { "a", a }, { "b", b } // will be ignored because b == null };
The intent was to facilitate construction of documents with optional elements, but in practice users have been confused and/or surprised by this. So we are going to change the Add method (which the above code implicitly calls) to throw an ArgumentNullException in this case.
You can easily achieve the same result just by adding an explicit test:
BsonValue a = 1; BsonValue b = null; var document = new BsonDocument { { "a", a }, { "b", b, b != null } // will be ignored because test is false };
The following is the complete list of methods that will be changed in this way:
BsonArray.Create (multiple overloads)
BsonArray.Add (multiple overloads)
BsonArray.AddRange (multiple overloads)
BsonBinaryData.Create (multiple overloads)
BsonBoolean.Create
BsonDateTime.Create
BsonDocument.Create
BsonDocument.Add (multiple overloads)
BsonDocument.Merge
BsonDouble.Create
BsonElement.Create (multiple overloads)
BsonInt32.Create
BsonInt64.Create
BsonJavaScript.Create (multiple overloads)
BsonJavaScriptWithScope.Create (multiple overloads)
BsonObjectId.Create(multiple overloads)
BsonRegularExpression.Create(multiple overloads)
BsonString.Create(multiple overloads)
BsonSymbol.Create(multiple overloads)
BsonTimeStamp.Create
- is related to
-
CSHARP-509 Remove superfluous Create methods and change how remaining Create methods handle C# null
- Closed
- related to
-
CSHARP-1287 DOCS- BsonValues no longer accept nulls in their ctors
- Closed