-1

I want to write

double num1;

if(num1 != double) 
{
Console.WriteLine("Invalid operator.");
}

But I get an error message saying "Invalid expression term 'double'" I want to have the if statement write "Invalid operator" if num1 isn't a double.

  • 4
    You want to what? Please elaborate, this doesn't make sense as-is. What are you expecting that `if` condition to do? –  Dec 26 '19 at 20:49
  • 5
    `num1` is a double. It cannot be anything else but a double. – bolov Dec 26 '19 at 20:59
  • 2
    It seems what you are looking for is `if (num1.GetType() != typeof(double))`. More information [here](https://stackoverflow.com/questions/11634079/how-can-i-get-the-data-type-of-a-variable-in-c/11634832) and [here](https://stackoverflow.com/questions/10415276/how-to-check-if-variables-type-matches-type-stored-in-a-variable). – Andrew Dec 26 '19 at 20:59
  • Alternatively, you can use the `is` operator: `if(num1 is double) { }`. – div Dec 26 '19 at 21:02
  • I think the edit helps and I've voted to reopen, but I think the earlier comments still make a very good point... What are you hoping to accomplish here? It should be incredibly clear that `num1` will be a double. You told it that it is, so the check won't gain you anything. – Broots Waymb Dec 26 '19 at 21:02
  • 2
    `I want to have the if statement write "Invalid operator" if num1 isn't a double.` Under what circumstances do you see `num1` *not* being a double? –  Dec 26 '19 at 21:06
  • @Div that works for this one case, but OP would need to be aware of implications a check like that can have with other types.The `is` operator will catch anything that extends the class (double is sealed, so it's fine here of course), so if something needs to be a specific type that will not work. – Broots Waymb Dec 26 '19 at 21:07

1 Answers1

3

The intention

Your question makes grammatical but not logical sense.

 double num1;

num1 is a double. It can't be anything but a double, since you explicitly declared it to be a double.

if(num1 != double) 

Bad syntax aside, since num1 is a double, the check is wholly unnecessary. It can never not be true, so there's no point in checking it.


The syntax

That being said, your syntax of num1 != double isn't valid. You're trying to compare the value of a variable to a given variable type. You're comparing apples and pears and the compiler has no idea how to handle that.

To compare types, you need to take the type of the variable, and compare it to another type. Something along the lines of:

public static void Main()
{
    double num1 = 0;

    if(num1.GetType() == typeof(double)) 
    {
        Console.WriteLine("num1 is a double");
    }
    else
    {
        Console.WriteLine("num1 is not a double, it is a " + num1.GetType().FullName);  
    }


    int num2 = 0;

    if(num2.GetType() == typeof(double)) 
    {
        Console.WriteLine("num2 is a double");
    }
    else
    {
        Console.WriteLine("num2 is not a double, it is a " + num2.GetType().FullName);  
    }
}

And the resulting output:

num1 is a double
num2 is not a double, it is a System.Int32

Link to example

Instead of num1.GetType() == typeof(double) you can also use num1 is double. It's cleaner code but they don't always work exactly the same. For double they are equivalent, but not if your chosen type could be derived (is would yield true on a derived type, but GetType() == typeof() would yield false)

But again, as I mentioned before, checking the type of a variable whose type you've explicitly defined makes no sense. Type checking is not a very common occurrence specifically because you often declare the type (implicitly or explicitly, it's the same to the compiler). It makes more sense when dealing with derived classes, when your variable is of a base type and you are interested if a particular derived type is being used.

Flater
  • 12,908
  • 4
  • 39
  • 62
  • _comparing apples and pears_ - it may be more apt to say, comparing apples to *fruit* - confusing an instance of a fruit, to the general type of fruit. – brnlmrry Dec 27 '19 at 00:09
  • @BriansaysReinstateMonica: I really agree with your correction, but its correctness (or rather, why it's pointing out an incorrect premise) is very unclear to beginners. "Is an apple a fruit" is correct English and essentially what OP is trying to find out - it just doesn't quite translate to code as literally as OP suspected.. – Flater Dec 27 '19 at 00:11
  • No problem at all, I agree it's a tough problem. The idiom just struck me as a bit funny. – brnlmrry Dec 27 '19 at 00:13