Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
If i call add(1.1); i am getting a compilation error - 'best overload method doesnot match'. But i have a float method in the code. why still i am getting this error ?? Sample code is given below,

C#
private void button1_Click(object sender, EventArgs e)
        {
            add(0.1);
        }

        void add(int i)
        {
            MessageBox.Show("int i");
        }

        void add(float i)
        {
            MessageBox.Show("float i");
        }


If i call add(1); i will get the answer 'int i' which is right.

If i call add(1.1); i am getting a compilation error - 'best overload method doesnot match'. But i have a float method in the code. why still i am getting this error ??

I f i write a method which has a 'double' argument compilation error goes.
Posted

1 solution

Hi,

1.1 is interpreted as a double, not as a float. You need to add suffix f for float (1.1f). Also, double has optional suffix d.

Here's equivalent example in a console application:
C#
class Program
{
    static void Main(string[] args)
    {
        add(1); // int
        add(1.1f); // float
        add(1.1); // double
        Console.ReadLine();
    }

    static void add(int i)
    {
        Console.WriteLine("int i");
    }

    static void add(float i)
    {
        Console.WriteLine("float i");
    }

    static void add(double i)
    {
        Console.WriteLine("double i");
    }
}

Result:
int i<br />
float i<br />
double i
 
Share this answer
 
v2
Comments
code4Better 3-Aug-14 15:32pm    
Thank you so much Andrius
Andrius Leonavicius 3-Aug-14 15:34pm    
You're welcome. :)
Sergey Alexandrovich Kryukov 3-Aug-14 22:05pm    
Correct, a 5. Another option could be switching to double type. :-)
—SA
Andrius Leonavicius 4-Aug-14 5:54am    
Thank you, Sergey. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900