[CSHARP-209] Implicit conversion of IEnumerable to BsonArray requested Created: 22/Apr/11  Updated: 02/Apr/15  Resolved: 23/Apr/11

Status: Closed
Project: C# Driver
Component/s: Feature Request
Affects Version/s: 1.0
Fix Version/s: 1.0

Type: New Feature Priority: Minor - P4
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

It would be nice if an implicit conversion from IEnumerable to BsonArray existed so we could write code like this:

var values = new int[]

{ 1, 2, 3 }

;
var document = new BsonDocument("array", values);

instead of:

var document = new BsonDocument("array", new BsonArray(values));

NOTE: make sure this implicit conversion doesn't have any undesirable side effects before implementing this request



 Comments   
Comment by Robert Stam [ 23/Apr/11 ]

Not implementable.

Comment by Robert Stam [ 23/Apr/11 ]

Since the C# language doesn't permit defining implicit conversion to or from interfaces this feature is not implementable. Other options (such as adding many new overloads of BsonDocument constructors and Add methods) are also not desirable.

One way in which the call to the BsonArray constructor can be avoided is by coding:

var values = new BsonArray

{ 1, 2, 3 }

; // instead of int[]
var document = new BsonDocument("array", values);

Otherwise a simple call to the BsonArray constructor is not that bad!

Comment by Robert Stam [ 22/Apr/11 ]

This might not be possible. Defining an implicit conversion from IEnumerable<int> to BsonArray results in a compiler error:

error CS0552: 'MongoDB.Bson.BsonArray.implicit operator MongoDB.Bson.BsonArray(System.Collections.Generic.IEnumerable<int>)': user-defined conversions to or from an interface are not allowed

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