I have two extension methods like this..
One is
public static BsonValue TopOne(this IEnumerable<BsonValue> IEnumBv)
{
}
Second One is
public static BsonValue TopOne(this BsonValue IEnumBv)
{
}
Is this not possible ? I am not getting reason.
Even name are same but its for different Extensions methods right ?
Or how to achieve this ? I need topone name for both the extensions.Is it possible ?
UPDATE
I am getting this error
The call is ambiguous between the following methods or properties: 'BsonExtensionLibrary.MongoExtensions.TopOne(MongoDB.Bson.BsonValue)' and 'BsonExtensionLibrary.MongoExtensions.TopOne(System.Collections.Generic.IEnumerable)
You can absolutely do this - but you'll have problems if you try to call the extension method on something which is both a BsonValue
and an IEnumerable<BsonValue>
. For example:
BsonArray array = ...;
BsonValue top = array.TopValue();
Which method would you expect to be called? Both are applicable, and neither conversion is better than the other.
Note that this has nothing to do with extension methods really - it's just overload resolution. You'd face exactly the same problem if those were normal static methods, and you had:
BsonArray array = ...;
BsonValue top = BsonHelpers.TopValue(array);
Either give the two methods different names, or find some way of expressing what you want, e.g.
BsonArray array = ...;
BsonValue topAsEnumerable = array.AsEnumerable().TopValue();
BsonValue topAsValue = ((BsonValue) array).TopValue();
(That's using the Enumerable.AsEnumerable
extension method just to convert the expression type to IEnumerable<BsonValue>
.)
See more on this question at Stackoverflow