[CSHARP-3229] Add BsonArray.Add(string, BsonValue) overload Created: 21/Oct/20  Updated: 26/Oct/20  Resolved: 26/Oct/20

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: 2.11.3
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Thiwakorn F. Assignee: Robert Stam
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

For simpler BsonArray initialization by taking advantage of C# collection initializer.

So we can simplify BsonArray initializing from this :

new BsonArray
{
    new BsonDocument("a", 1),
    new BsonDocument("b", 2),
};

To this :

new BsonArray
{
    { "a", 1 },
    { "b", 2 },
};



 Comments   
Comment by Robert Stam [ 26/Oct/20 ]

While this initially sounds like a good idea I don't think we should do this, for several reasons:

1. It is not visually distinctive from just adding items to the array:

var array = new BsonArray { "a", 1, "b", 2, { "c", 3 }, "d", 4 }; // looks very much like an 8-element array

2. It only works for nested documents that have exactly one element

var array = new BsonArray { new BsonDocument { { "x", 1 }, { "y", 2 } } }; // no way to do this with the proposed syntax

3. It doesn't generalize to nested arrays

var array = new BsonArray { 1, 2, [3, 4], 5}; // invalid syntax

 

Generated at Wed Feb 07 21:44:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.