0

Im trying to create a method with a calculation and then call it inside two other methods. The problem I have is that each of the other two methods are int and float respectively.

i want to create a method of this "((celsius * 9) / 5) + 32;" and then call it in overloading 1 and 2, problem is the first is int and the second is float.. CTF is the method i tried to create for this.

How do I solve this? Thanks!

        // Overloading 1 - with float
        public static int CelsiusToFahrenheit(int celsius)
        {
            int fahrenheit = ((celsius * 9) / 5) + 32;
            return fahrenheit;
        }

        //METHOD, CelsiusToFahrenheit
        //Overloading 2 - with float
        public static float CelsiusToFahrenheit(float celsius)
        {
            int fahrenheit = ((celsius * 9) / 5) + 32;
            return fahrenheit;
        }

        //METHOD, convert celsius to fahrenheit
        static double CTF(double celsius)
        {
            
            double a = ((celsius * 9) / 5) + 32;
            return a; 

        }

  • Looks like a fit for [generic methods](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-methods). – Sinatr Nov 03 '20 at 11:25
  • 2
    @Sinatr: I don't think so. Generics don't really help with numeric operations like this. – Jon Skeet Nov 03 '20 at 11:30
  • 1
    You haven't really told us what the problem is - can you not just cast within each public method, e.g. `public static int CelsiusToFahrenheit(int celsius) => (int) CTF(celsius);` and likewise for the `float` overload? – Jon Skeet Nov 03 '20 at 11:30
  • @JonSkeet, not sure, but [it works for me](https://dotnetfiddle.net/imtv2D). – Sinatr Nov 03 '20 at 11:48
  • 2
    I would define only one method, the one with `double`. An input parameter of `int` or `float` will automatically be converted to `double`. Then, if you need another result type, just cast: `int result = (int)CTF(17.2);`. This is what you have to do the most methods in `System.Math`: `int root = (int)Math.Sqrt(x);`. (Note that there is also a `MathF` class for floats in .NET Core – Olivier Jacot-Descombes Nov 03 '20 at 11:52
  • @Sinatr: The point of generic is to not have to cast or convert. So the API is generic but the inside is not very generic. In this case, you are better off with 3 overloaded non-generic methods. If all 3 methods of the OP had the same name and were public, this would work well. – Olivier Jacot-Descombes Nov 03 '20 at 12:01
  • 1
    @Sinatr: Using a cast and `Convert.ChangeType` pretty much defeats the point of using generics though. It's meant to provide compile-time safety, but `CTF(DateTime.UtcNow)` compiles and throws an exception with your code. – Jon Skeet Nov 03 '20 at 12:12
  • @JonSkeet, just trying to defend my initial comment. I've seen worse solutions with `dynamic` or `switch/case`, otherwise you are totally right, generics [won't help](https://stackoverflow.com/q/2645301/1997232) here. – Sinatr Nov 03 '20 at 12:39

0 Answers0