Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
title: "== Operator - C# Reference"
ms.custom: seodec18

ms.date: 07/20/2015
ms.date: 12/14/2018
f1_keywords:
- "==_CSharpKeyword"
helpviewer_keywords:
Expand All @@ -11,16 +10,48 @@ helpviewer_keywords:
ms.assetid: 34c6b597-caa2-4855-a7cd-38ecdd11bd07
---
# == Operator (C# Reference)
For predefined value types, the equality operator (`==`) returns true if the values of its operands are equal, `false` otherwise. For reference types other than [string](../../../csharp/language-reference/keywords/string.md), `==` returns `true` if its two operands refer to the same object. For the `string` type, `==` compares the values of the strings.

## Remarks
User-defined value types can overload the `==` operator (see [operator](../../../csharp/language-reference/keywords/operator.md)). So can user-defined reference types, although by default `==` behaves as described above for both predefined and user-defined reference types. If `==` is overloaded, [!=](../../../csharp/language-reference/operators/not-equal-operator.md) must also be overloaded. Operations on integral types are generally allowed on enumeration.

## Example
[!code-csharp[csRefOperators#36](../../../csharp/language-reference/operators/codesnippet/CSharp/equality-comparison-operator_1.cs)]

## See Also

- [C# Reference](../../../csharp/language-reference/index.md)
- [C# Programming Guide](../../../csharp/programming-guide/index.md)
- [C# Operators](../../../csharp/language-reference/operators/index.md)

The equality operator `==` returns `true` if its operands are equal, `false` otherwise.

## Value types equality

Operands of the [built-in value types](../keywords/value-types-table.md) are equal if their values are equal:

[!code-csharp-interactive[value types equality](~/samples/snippets/csharp/language-reference/operators/EqualityAndNonEqualityExamples.cs#ValueTypesEquality)]

Two operands of the same [enum](../keywords/enum.md) type are equal if the corresponding values of the underlying integral type are equal.

By default, the `==` operator is not defined for a user-defined [struct](../keywords/struct.md) type. A user-defined type can [overload](#operator-overloadability) the `==` operator.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should state that == is defined for enum types. That slipped through the cracks in the new (more clear) description.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BillWagner indeed, as behavior for structs is described, enum case also should be stated explicitly. Thank you for the remark. I've added the clarification, please check the latest commit.

Copy link
Member

@BillWagner BillWagner Dec 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkulikov I'll suggest this small word change:

"Two operands of the same enum type are equal if the ..."

It's a small change, but the text you have could be misinterpreted to support something like the following:

    enum Color
    {
        Red,
        Green,
        Blue
    }

    enum Status
    {
        Red,
        Yellow,
        Green
    }

// compare two enum operands:
 bool equal = (Status.Red == Color.Red); // CS0019 (operator == cannot be applied to ...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BillWagner that's a good catch! Thanks. Fixed.


Beginning with C# 7.3, the `==` and [`!=`](not-equal-operator.md) operators are supported by C# [tuples](../../tuples.md). For more information, see the [Equality and tuples](../../tuples.md#equality-and-tuples) section of the [C# tuple types](../../tuples.md) article.

## String equality

Two [string](../keywords/string.md) operands are equal when both of them are `null` or both string instances are of the same length and have identical characters in each character position:

[!code-csharp-interactive[string equality](~/samples/snippets/csharp/language-reference/operators/EqualityAndNonEqualityExamples.cs#StringEquality)]

That is case-sensitive ordinal comparison. For more information about how to compare strings, see [How to compare strings in C#](../../how-to/compare-strings.md).

## Reference types equality

Two other than `string` reference type operands are equal when they refer to the same object:

[!code-csharp-interactive[reference type equality](~/samples/snippets/csharp/language-reference/operators/EqualityAndNonEqualityExamples.cs#ReferenceTypesEquality)]

The example shows that the `==` operator is supported by user-defined reference types. However, a user-defined reference type can overload the `==` operator. If a reference type overloads the `==` operator, use the <xref:System.Object.ReferenceEquals%2A?displayProperty=nameWithType> method to check if two references of that type refer to the same object.

## Operator overloadability

User-defined types can [overload](../keywords/operator.md) the `==` operator. If a type overloads the equality operator `==`, it must also overload the [inequality operator](not-equal-operator.md) `!=`.

## C# language specification

For more information, see the [Relational and type-testing operators](~/_csharplang/spec/expressions.md#relational-and-type-testing-operators) section of the [C# language specification](../language-specification/index.md).

## See also

- [C# Reference](../index.md)
- [C# Programming Guide](../../programming-guide/index.md)
- [C# Operators](index.md)
- [Equality comparisons](../../programming-guide/statements-expressions-operators/equality-comparisons.md)
39 changes: 22 additions & 17 deletions docs/csharp/language-reference/operators/not-equal-operator.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
title: "!= Operator - C# Reference"
ms.custom: seodec18

ms.date: 07/20/2015
ms.date: 12/14/2018
f1_keywords:
- "!=_CSharpKeyword"
helpviewer_keywords:
Expand All @@ -12,18 +11,24 @@ helpviewer_keywords:
ms.assetid: eeff7a4e-ad6f-462d-9f8d-49e9b91c6c97
---
# != Operator (C# Reference)
The inequality operator (`!=`) returns false if its operands are equal, true otherwise. Inequality operators are predefined for all types, including string and object. User-defined types can overload the `!=` operator.

## Remarks
For predefined value types, the inequality operator (`!=`) returns true if the values of its operands are different, false otherwise. For reference types other than `string`, `!=` returns true if its two operands refer to different objects. For the `string` type, `!=` compares the values of the strings.

User-defined value types can overload the `!=` operator (see [operator](../../../csharp/language-reference/keywords/operator.md)). So can user-defined reference types, although by default `!=` behaves as described above for both predefined and user-defined reference types. If `!=` is overloaded, [==](../../../csharp/language-reference/operators/equality-comparison-operator.md) must also be overloaded. Operations on integral types are generally allowed on enumeration.

## Example
[!code-csharp[csRefOperators#33](../../../csharp/language-reference/operators/codesnippet/CSharp/not-equal-operator_1.cs)]

## See Also

- [C# Reference](../../../csharp/language-reference/index.md)
- [C# Programming Guide](../../../csharp/programming-guide/index.md)
- [C# Operators](../../../csharp/language-reference/operators/index.md)

The inequality operator `!=` returns `true` if its operands are not equal, `true` otherwise. For the operands of the [built-in types](../keywords/built-in-types-table.md), the expression `x != y` produces the same result as the expression `!(x == y)`. For more information, see the [== Operator](equality-comparison-operator.md) article.

The following example demonstrates the usage of the `!=` operator:

[!code-csharp-interactive[non-equality examples](~/samples/snippets/csharp/language-reference/operators/EqualityAndNonEqualityExamples.cs#NonEquality)]

## Operator overloadability

User-defined types can [overload](../keywords/operator.md) the `!=` operator. If a type overloads the inequality operator `!=`, it must also overload the [equality operator](equality-comparison-operator.md) `==`.

## C# language specification

For more information, see the [Relational and type-testing operators](~/_csharplang/spec/expressions.md#relational-and-type-testing-operators) section of the [C# language specification](../language-specification/index.md).

## See also

- [C# Reference](../index.md)
- [C# Programming Guide](../../programming-guide/index.md)
- [C# Operators](index.md)
- [Equality comparisons](../../programming-guide/statements-expressions-operators/equality-comparisons.md)