c# – Convert double to float by cast or Convert.ToSingle()?

c# – Convert double to float by cast or Convert.ToSingle()?

From the .NET reference source:

public static float ToSingle(double value)
{
     return (float)value;
}

So, your answer is that they are exactly the same, under the hood.

Any preference between the two is strictly a personal style choice. Personally, I would always use the cast as it is shorter and and just seems more idiomatic to me.

c# – Convert double to float by cast or Convert.ToSingle()?

Leave a Reply

Your email address will not be published.