Why does is operator behave like == operator?

is it true that if two objects return true for is operator they will also return true for operator
python is
difference between = and == in python
what is operator
python string comparison is vs
python when to use is
python is equal

Assume we have statement like this:

enum EngineType

var engine = EngineType.Electric;

if (engine is EngineType.Electric)
    // do something

Why does if statement is true. From microsoft documentation: The is operator checks if the result of an expression is compatible with a given type.

Isn't the left value type of EngineType (enum) and right value type of integer? Am I missing something?

What is the difference between 'is' and '==' in python, 'is' and '==' operators in Python The == operator is used when the values of two operands are equal, then the condition becomes true. The is operator evaluates to true if the variables on either side of the operator point to the same object and false otherwise. "and why do they behave like value type ?" Overloading the == operator doesn't mean that the type behaves like a value type. There's no connection between value types and the == operator beyond the fact that value types usually do overload == and reference types usually do not overload ==.

Because when you use the is keyword this way, you are actually doing Pattern Matching (starting with C# 7.0) :

Constant pattern, which tests whether an expression evaluates to a specified constant value.

The Difference Between “is” and “==” in Python – dbader.org, Python has two operators for equality comparisons, “is” and “==” (equals). you want to understand how Python's is and == comparison operators behave. Why operator[] returns a reference. Let’s take a closer look at how list[2] = 3 evaluates. Because the subscript operator has a higher precedence than the assignment operator, list[2] evaluates first. list[2] calls operator[], which we’ve defined to return a reference to list.m_list[2].

Enums are full types. While internally they are represented and can be (implicitly?) cast into integers (the number type they use/equate to can be changed), they are each a distinct type.

This actually enables all those Additional typecheks that make them useful. We recently had a Question about why we do not just use int constants over Enums:

Another enum vs. int

is compares types and only types. If you do not give it a type, it will just go and find the type of what you did give it. With the values primitive types it can be ambigious. It is a compiler/runtime detail if 1 is a Int16, Int32 or Int64 today. Indeed it will likely varry on the digit lenght of the constant I give it. Not something I can rely on.

It could propably tell you that 1.2 is not the same as type 1 (unless some braindead designer decided to use decimal or float values in both cases for once).

But all possible values of the type EngineType are of the type EngineType. That is a big reasons Enums are a thing in the first place. Exactly to allow such type checks.

Understanding Comparison Operators “is” and “==” in Python, is crucial in understanding how Python's is and == operators behave. The “ ==” operator compares by checking for equality: if we compare the two are equal” as an answer, since they are carrying the same name value. From :h operator. After applying the operator the cursor is mostly left at the start of the text that was operated upon. For example, "yfe" doesn't move the cursor, but "yFe" moves the cursor leftwards to the "e" where the yank started. What's happening is that the cursor is being moved to the beginning of the text object. Enabling visual mode

Python '!=' Is Not 'is not': Comparing Objects in Python – Real Python, The == operator compares the value or equality of two objects, whereas Why using is and is not to compare values leads to unexpected behavior Python Pit Stop: This tutorial is a quick and practical way to find the info you Use the equality operators == and != if you want to check whether or not two� It is one of the things where Java and Python are fundamentally different. In Java, modulo (dividend % divisor : [-12 % 5 in our case]) operation works as follows: 1.

Equality operators: == and !=, The equality operators, equal to ( == ) and not equal to ( != ), have lower precedence than the relational operators, but they behave similarly. So What’s an Operator? An operator is a function you pass into a pipe. And pipe returns its own observable. So let’s think about what that means: An operator has the original observable as the first argument; An operator returns an observable; This most basic operator we can write looks like this:

Difference Between == Operator and Equals Method In C#, Generally, the == operator is a comparison between similar C# primitive types While they look like and behave like primitives, they are not. Well why so ? Because it (Spread Operator) does till the first level, whenever it finds the nested level, it doesn’t do the Deep Copy. It does the Shallow Copy of the Object from next level onwards . In summary Spread Operator (written as …) does the Shallow Copy(for first level it does but for others it doesn’t).

  • Which version are you using? Attempting to compile your example results in a "The type name 'Electric' does not exist in the type 'EngineType'" compilation error, which I would expect since EngineType.Electric is not a type.
  • @Lee I'm working with VS2019 C#7.0 so that must be it.
  • C# pattern matching in action?
  • @TanveerBadar: Precisely. This is a constant pattern.
  • I can't get this to compile, it is explicitly breaking on the is MyEnum.MyValue syntax. Am I missing something or is this not valid syntax?
  • @Flater False. You can use this in a .NET Framework that targets even as far back as .NET 2.0 if you wanted. You just have to be using a compiler that supports C# 7.0.
  • @Herohtar: Dotnetfiddle runs .Net 4.7.3 though so I'm expecting it to be C#7.3 (same release). But Dotnetfiddle doesn't explicitly mention the C# version, granted.
  • @Flater There's no telling what Dotnetfiddle is running behind the scenes, so you can't base anything on what does or doesn't work there. I can confirm that the syntax works when I create a .NET Framework 2.0 project using Visual Studio 2019.
  • Enums are types but EngineType.Electric is not a type so this doesn't fully answer the question.