-5

I ma newbie to C# and this piece of code I am trying to use double as data type

using System;

namespace FirstProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "My First Project";

            double num1, num2, div;

            Console.WriteLine("Enter The First Number :");
            num1 = double.Parse(Console.ReadLine());

            Console.WriteLine("Enter The Second Number :");
            num2 = double.Parse(Console.ReadLine());

            switch (num2)
            {
                case 0:
                Console.WriteLine("Alert Divide By Zero");
                    break;
                default:
                     div = num1 / num2;
                    Console.WriteLine("The Result Of Division Is " + div);
                    break;
            }          

            Console.ReadKey();
        }
    }
}

The variable num2 has an alert that Error 1 A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type C:\Users\Future\documents\visual studio 2012\Projects\FirstProject\FirstProject\Program.cs 19 21 FirstProject .. How can I use double in the switch expression?

There is no specific details. The code simply uses two numbers entered by the user then check for the second number. If the second number is zero, alert dislayed else the division result is displayed. (this is for those who asked for more details) When using int instead if double, no errors appeared but when using double to get the result of division as double, I got the error I displayed earlier.

YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
  • 2
    Why not just use an `if`? You are only checking for one condition. – Johnathan Barclay Oct 05 '20 at 08:39
  • I am practising the use of Switch. – YasserKhalil Oct 05 '20 at 08:40
  • 7
    You cannot use floating point numbers in `switch` expressions because due to numerical inaccuracies direct equality comparisons are a really bad idea – UnholySheep Oct 05 '20 at 08:40
  • You can't switch on a double. As the error message says, you can only switch on one of those data types. – jeroenh Oct 05 '20 at 08:41
  • Could you use some words to describe what you want to achieve... Presumably you've read the error message and understand that shown code that can't work is rarely enough to show what you want to acthieve... – Alexei Levenkov Oct 05 '20 at 08:41
  • Thanks a lot for replies. Is there a workaround to deal with that case? – YasserKhalil Oct 05 '20 at 08:41
  • 7
    "_I am practising the use of Switch_" A switch is the wrong tool for this job. – Johnathan Barclay Oct 05 '20 at 08:42
  • you should use integer instead, makes more sense. – pbachman Oct 05 '20 at 08:42
  • 1
    Actually starting C# 7.0 any non-null expression could be used for switch: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch#the-match-expression. Update framework version for the project and try to use double. – Aleksei Petrov Oct 05 '20 at 08:43
  • Thanks a lot. So in that case if I need to use double, I can't use Switch (That what I Have learnt)..So Switch is limited ??! – YasserKhalil Oct 05 '20 at 08:43
  • 3
    @YasserKhalil `switch` is a procedural construct anyway. It only has a small set of use cases in an OO context. – Johnathan Barclay Oct 05 '20 at 08:45
  • `double` is an awful selector... Anything but literal values can't be really compared for equality - if you do any calculations there is pretty much no chance you get exact values you are looking to match... So what are you trying to practice? Clearly version of C# you are using does not support that... and any newer version will not produce *useful results*... Still clarification of what you want to achieve would be nice. – Alexei Levenkov Oct 05 '20 at 08:51
  • 1
    I'm not sure what part of *A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type* you didn't understand? That seems pretty self explanatory? – Liam Oct 05 '20 at 09:18

1 Answers1

5

For C# language before 7.0 switch is limited to bool, char, string, integral, enum, or corresponding nullable type. C# language version starting 7.0 switch statement supports any non-null expression. Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch#the-match-expression

If you really want to use double in switch, please check framework version you use and language version in project properties. C# 7.0 requires at least VS 2017 and .NET 4.7 as the Target Framework.

I've try it with .NET Core 3.1, and it works with doubles:

double num = 1.0;
switch (num)
{
    case 0.0:
        Console.WriteLine("1");
        break;
    default:
        Console.WriteLine("2");
        break;
}

But, as others already pointed out, this is really weird way to use switch.

Aleksei Petrov
  • 936
  • 12
  • 31
  • Don't worry. If you need some further assistance with this problem, I could help you in chat. But for doubles in switch you need to install at least VS 2017. – Aleksei Petrov Oct 05 '20 at 10:15
  • @YasserKhalil Please read https://stackoverflow.com/q/1398753/529282 It's very likely you don't want to use error prone floating point comparison – Martheen Oct 05 '20 at 10:17