Temperature converter using "switch" statement I wrote a program to convert temperature into different units, where the users have to put a space between temperature and unit:


  23 f, 34 c, 45 k


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PracticeSet1
{

    class Program
    {
        public static float temp;
        public static char tempUnit;


        static void Main(string[] args)
        {
            //Getting user input 
            Console.WriteLine("Enter Temprature in to convert it into other i.e  30 k, 45 f, 50 c *Put space between value and unit* ");
            string[] tempInput = Console.ReadLine().Split();

            //parse element 0
            temp = float.Parse(tempInput[0]);
            //assinging tempUnit 
            tempUnit = char.Parse(tempInput[1]);

            switch (tempUnit)
            {
                //Converting temp to F and K if tempUnit == c
                case 'c':
                    Console.WriteLine("Celsius To Farhenheit and Kelvin");
                    convertCelsiusToFarhenheit();
                    convertCelsiusToKelvin();
                    break;
                //Converting temp to C and F if tempUnit == K

                case 'k':
                    Console.WriteLine("Kelvin");
                    convertKelvinToCelsius();
                    convertKelvinToFarhenheit();
                    break;
                //Converting temp to C and K if tempUnit == F

                case 'f':
                    Console.WriteLine("Farhenheit to Celsius and kelvin");
                    convertFarhenheitToCelsius();
                    convertFarhenheitToKelvin();
                    break;

            }




        }
        static void convertFarhenheitToCelsius()
        {
            Console.WriteLine((temp - 32) * 0.5556 + "°C");
        }
        static void convertFarhenheitToKelvin()
        {
            Console.WriteLine((temp + 459.67) * 5 / 9 + "°K");
        }
        static void convertCelsiusToFarhenheit()
        {
            Console.WriteLine((temp * 1.8) + 32 + "°F");
        }
        static void convertCelsiusToKelvin()
        {
            Console.WriteLine(temp + 273.15 + "°K");

        }
        static void convertKelvinToCelsius()
        {
            Console.WriteLine(temp - 273.15 + "°C");
        }
        static void convertKelvinToFarhenheit()
        {
            Console.WriteLine(temp - 459.67 + "°F");
        }
    }
}
public static float temp;
public static char tempUnit;


There's no reason to make these into global variables. It's better to use local variables:

static void Main(string[] args)
{
    float temp;
    char tempUnit;
}




If you want a first step into OOP, try wrapping the temp and tempUnit into a class (or struct) of their own.



While Console.ReadLine().Split() is essentially the same as Console.ReadLine().Split(' '), I would suggest putting the character in there. 

It's a matter of readability. Initially, I assumed it would split on newlines, not spaces, until I remembered. Or, alternatively, add a comments that mentions you're splitting on spaces.



You're not handling garbage values. What happens if the user enters 123f or batman or k 123? Try to handle invalid data and show the user an error message that describes the problem.

if(!float.TryParse(tempInput[0], out temp)
{
     Console.WriteLine($"Cannot parse the value \"{tempInput[0]}\"");

     //Wait until user presses enter and close the application
     Console.ReadLine();
     return;
}


The subsequent code is the same, as TryParse will still put a usable value in the temp variable.



Console.WriteLine(temp + 273.15 + "°K");


While this currently works, I highly suggest not doing this.

It's very hard to spot that you're first doing a mathematical operation (number + number), and then a string concatenation (number + string, which is handled as string + string).

Suppose you're in a culture where the unit comes before the value (e.g. °K 123). You can't just simply invert the code ("°K" + temp + 273.15); because if temp equals 123, your result will be °K123273.15 because it is now concatenating three string values.
While you could work around this issue with parentheses, it's still not a good fix.

There's no reason to inline all statement. Especially when you're a beginner, but this applies to experts as well. Handling the same operations on multiple lines does not affect application performance, but it does dramatically lower readability.

var convertedValue = temp + 273.15;
var convertedValueString = convertedValue + "°K";


Notice that I didn't use Console.WriteLine() yet. You've had to copy paste that in every method, which is not a reusable pattern.

It would be better to have the method return a string:

static string convertCelsiusToKelvin()
{
    var convertedValue = temp + 273.15;
    var convertedValueString = convertedValue + "°K";
    return convertedValueString;
}


And wrap the method call in the Main() method:

switch (tempUnit)
{
    //Converting temp to F and K if tempUnit == c
    case 'c':
        Console.WriteLine("Celsius To Farhenheit and Kelvin");
        Console.WriteLine(convertCelsiusToFarhenheit());
        Console.WriteLine(convertCelsiusToKelvin());


Note: This can be optimized further, but this is already a good first step. It teaches you to separate the calculation from the presentation of the calculated value.



Try to separate the logic into methods more. Your Main() method currently is doing many things that don't really have anything to do with each other:


Read the values
Decide which conversion calculations to do
(As per my suggestion) Printing the converted values.


A minor redesign to show you what I mean. First, let's define a struct for our values:

public struct Temperature
{
    float Value;
    char Unit;
}


You could redesign your main method to be much more abstracted:

static void Main(string[] args)
{
     Temperature input = GetInputFromUser();

     Temperature valueKelvin = CalculateKelvin(input);
     Print(valueKelvin);

     Temperature valueFahrenheit = CalculateFahrenheit(input);
     Print(valueFahrenheit);

     Temperature valueCelsius = CalculateCelsius(input);
     Print(valueCelsius);

    Console.ReadLine();
}


Some explanatory examples:

public static Temperature CalculateKelvin(Temperature inputValue)
{
     switch(inputValue.Unit)
     {
         case 'K':
             //Same unit, so no conversion needed
             return value;
         case 'F':
             // F =&gt; K
             return convertFahrenheitToKelvin(inputValue);
         case 'C':
             // C =&gt; K
             return convertCelsiusToKelvin(inputValue);

     }
}

public static void Print(Temperature value)
{
    Console.WriteLine($"{value.Value} °{value.Unit}");
}