Double & Float method parameters execution when called in C#
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
{
s开发者_如何学Pythontatic 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
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.
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.
精彩评论