Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the C# 'is' operator give a correct result when comparing two boolean values and should I use it?

Tags:

c#

c#-7.0

Id noticed that this is operator produces the same results and even compiles the same (see this gist) as the == operator. Is this a correct usage of the is operator given that I can only see things related to runtime type-testing in the docs?

bool test = false;

if(!test)
{
// some code
}

//OR

if(test is false)
{
// some code
}
like image 455
PontiusTheBarbarian Avatar asked Sep 10 '21 09:09

PontiusTheBarbarian


People also ask

Why does the C exist?

Like the letter G, C emerged from the Phoenician letter gimel (centuries later, gimel became the third letter of the Hebrew alphabet). In ancient Rome, as the Latin alphabet was being adapted from the Greek and Etruscan alphabets, G and C became disambiguated by adding a bar to the bottom end of the C.

Does the letter C need to exist?

This is a very important rule considering about 25% of words in our language contain a C.] So why do we need a C? When we combine the C with an H we DO make a unique sound. Without a C we would go to Hurch instead of Church, we would listen to a Hime instead of a Chime, etc.

Is the letter C useless?

But yeah, here are all the letters from most useless to most useful: X, C, Q, Y, W, H, Z, V, B, D, G, P, E, M, L, U, J, R, F, N, K, A, I, T, S, O. I hope you enjoyed this.

Why does C have the sound of S?

Here's the rule: When 'c' comes directly before the letters 'e', 'i' or 'y' we use the /s/ sound. in other cases we use a /k/ sound.

Why should I learn C?

Moreover, a lot of the principles used in C – for instance, argc and argv for command line parameters, as well as loop constructs and variable types – will show up in a lot of other languages you learn so you’ll be able to talk to people even if they don’t know C in a way that’s common to both of you.

What is the difference between C and C++ and C?

C has a very small runtime. And the memory footprint for its code is smaller than for most other languages. When compared to C++, for example, a C-generated binary that goes to an embedded device is about half the size of a binary generated by similar C++ code.

Why do most notes start with C?

Notes do not "start" with C; C major is just the easiest major key to notate in modern notation. The concept of a major key came about long after letters were assigned to the notes. Before there were major (and minor) keys, people used modes, usually just using the notes of the modern white keys and starting and ending in different places.

Why is the world powered by C?

Many C projects are still started today; there are some good reasons for that. How is the World Powered by C? Despite the prevalence of higher-level languages, C continues to empower the world. The following are some of the systems that are used by millions and are programmed in the C language.


1 Answers

Yes, it's completely valid - it's a constant pattern. In that sense, it's "correct". It's not an implementation detail or bug on the part of the compiler.

It's fairly unconventional to use it this way within an if statement though - constant patterns are more commonly used in switch statements or expressions.

They're not completely equivalent though. Pattern matching with is doesn't perform custom conversions on the left-hand side of the is operator. So take this example:

using System;
using System.Text;

struct MyBool
{
    public bool Value { get;}
    
    public MyBool(bool value) => Value = value;
    
    public static implicit operator bool(MyBool value) => value;
}

class Test
{
    static void Main()
    {
        MyBool x = new MyBool(true);
        // Valid: uses the implicit conversion from MyBool to bool
        Console.WriteLine(x == true);
        // Invalid
        Console.WriteLine(x is true);
    }
}

The error message for the last statement is perhaps a little confusing:

error CS0029: Cannot implicitly convert type 'bool' to 'MyBool'

That's due to this part of the constant pattern behaviour:

When the input value is not an open type, the constant expression is implicitly converted to the type of the matched expression; if the type of the input value is not pattern-compatible with the type of the constant expression, the pattern-matching operation is an error.

Interestingly, if you add a conversion from bool to MyBool, the error changes:

error CS0150: A constant value is expected

But fundamentally it's not valid, anyway.

like image 192
Jon Skeet Avatar answered Oct 24 '22 06:10

Jon Skeet