Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous between methods or properties in C# .NET

Tags:

c#

int n = 5;
int quorum = Math.Floor(n / 2) + 1;

I'm expecting quorum to have value 3. But this is the error I get in VisualStudio:

The call is ambiguous between the following methods or properties: 'System.Math.Floor(double)' and 'System.Math.Floor(decimal)'

How do I correct it? Where did I go wrong?

like image 408
clewaks Avatar asked Apr 13 '12 00:04

clewaks


People also ask

Where call is ambiguous between the following methods?

The call is ambiguous between the following methods or properties: 'Assert. That(ActualValueDelegate, IResolveConstraint)' and 'Asser - Visual Studio Feedback.


1 Answers

You have no need to use Math.Floor. Because all of your terms are integers, .NET will perform integer division which automatically truncates the remainder of the output anyway.

As for why you're getting the error, as stated above the result of integer division is still an integer. Because you can't floor an integer (there's no fractional component to round down), there's no overload of Floor that takes an int. The call would have to convert the result to a decimal or double first, and the compiler doesn't know which one you want (which is, in fact, neither).

like image 93
Adam Robinson Avatar answered Sep 21 '22 18:09

Adam Robinson