Will the differences below matter significantly in C#?
int a, b;
double result;
result = (double)a / b;
result = a / (double)b;
result = (double)a / (double)b;
Which one do you use?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The cast will occur before the division.
In your examples, it doesn’t matter which one you do as if one operand is a double, the runtime will cast/convert the other to a double as well.
This looks like a micro-optimization – not something worth worrying about or dealing with unless measurements show it is indeed a bottleneck.