views:

227

answers:

2

Hi there.

Here's a small snippet of code, when called it outputs 'double'. Why? What's the reasoning behind this. Why doesn't it print 'float'?

class source
{

    static void Main()
    {
        Receiver r = new Receiver();


        r.Method1(1.1);
    }

}

class Receiver
{
    public virtual void Method1(double f) { Debug.Print("double"); }
    public virtual void Method1(float f) { Debug.Print("float"); }
}

TIA

+12  A: 

To specify float call like this:

r.Method1(1.1f);

Otherwise it'll default to double, like you observed.

Here's a porition of the MSDN documentation on double that explains why:

By default, a real numeric literal on the right-hand side of the assignment operator is treated as double.

Jay Riggs
thanks jay but why does it default to double?
SoftwareGeek
By design. See my edit.
Jay Riggs
+5  A: 

double is the default type for non integers. So 1.1 is a double, 1.1m is a decimal and 1.1F is a float.

Mike Two