diff --git a/docs/csharp/language-reference/operators/assignment-operator.md b/docs/csharp/language-reference/operators/assignment-operator.md index 367b0f6843b94..47051e545e939 100644 --- a/docs/csharp/language-reference/operators/assignment-operator.md +++ b/docs/csharp/language-reference/operators/assignment-operator.md @@ -1,6 +1,6 @@ --- title: "= Operator (C# Reference)" -ms.date: 07/20/2015 +ms.date: 10/31/2018 f1_keywords: - "=_CSharpKeyword" helpviewer_keywords: @@ -8,16 +8,35 @@ helpviewer_keywords: ms.assetid: d802a6d5-32f0-42b8-b180-12f5a081bfc1 --- # = Operator (C# Reference) -The assignment operator (`=`) stores the value of its right-hand operand in the storage location, property, or indexer denoted by its left-hand operand and returns the value as its result. The operands must be of the same type (or the right-hand operand must be implicitly convertible to the type of the left-hand operand). - -## Remarks - The assignment operator cannot be overloaded. However, you can define implicit conversion operators for a type, which enable you to use the assignment operator with those types. For more information, see [Using Conversion Operators](../../../csharp/programming-guide/statements-expressions-operators/using-conversion-operators.md). - -## Example - [!code-csharp[csRefOperators#49](../../../csharp/language-reference/operators/codesnippet/CSharp/assignment-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 assignment operator `=` assigns the value of its right-hand operand to a variable, a [property](../../programming-guide/classes-and-structs/properties.md), or an [indexer](../../../csharp/programming-guide/indexers/index.md) element given by its left-hand operand. The result of an assignment expression is the value assigned to the left-hand operand. The type of the right-hand operand must be the same as the type of the left-hand operand or implicitly convertible to it. + +The assignment operator is right-associative, that is, an expression of the form + +```csharp +a = b = c +``` + +is evaluated as + +```csharp +a = (b = c) +``` + +The following example demonstrates the usage of the assignment operator to assign values to a local variable, a property, and an indexer element: + +[!code-csharp-interactive[assignment operator](~/samples/snippets/csharp/language-reference/operators/AssignmentExamples.cs#Assignments)] + +## Operator overloadability + +A user-defined type cannot overload the assignment operator. However, a user-defined type can define an implicit conversion to another type. That way, the value of a user-defined type can be assigned to a variable, a property, or an indexer element of another type. For more information, see the [implicit](../keywords/implicit.md) keyword article. + +## C# language specification + +For more information, see the [Simple assignment](~/_csharplang/spec/expressions.md#simple-assignment) 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) diff --git a/docs/csharp/language-reference/operators/codesnippet/CSharp/assignment-operator_1.cs b/docs/csharp/language-reference/operators/codesnippet/CSharp/assignment-operator_1.cs deleted file mode 100644 index 43e2a68fd4820..0000000000000 --- a/docs/csharp/language-reference/operators/codesnippet/CSharp/assignment-operator_1.cs +++ /dev/null @@ -1,23 +0,0 @@ - class Assignment - { - static void Main() - { - double x; - int i; - i = 5; // int to int assignment - x = i; // implicit conversion from int to double - i = (int)x; // needs cast - Console.WriteLine("i is {0}, x is {1}", i, x); - object obj = i; - Console.WriteLine("boxed value = {0}, type is {1}", - obj, obj.GetType()); - i = (int)obj; - Console.WriteLine("unboxed: {0}", i); - } - } - /* - Output: - i is 5, x is 5 - boxed value = 5, type is System.Int32 - unboxed: 5 - */ \ No newline at end of file