Advertisement

Responsive Advertisement

V- Assignment Operator

 

V- Assignment Operator

The assignment operator = assigns the value of its right-hand operand to a variable, a property, or an indexer 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 following example demonstrates the usage of the assignment operator with a local variable, a property, and an indexer element as its left-hand operand:

var numbers = new List<double>() { 1.0, 2.0, 3.0 };

Console.WriteLine(numbers.Capacity);
numbers.Capacity = 100;
Console.WriteLine(numbers.Capacity);
// Output:
// 4
// 100

int newFirstElement;
double originalFirstElement = numbers[0];
newFirstElement = 5;
numbers[0] = newFirstElement;
Console.WriteLine(originalFirstElement);
Console.WriteLine(numbers[0]);
// Output:
// 1
// 5

The left-hand operand of an assignment receives the value of the right-hand operand. When the operands are of value types, assignment copies the contents of the right-hand operand. When the operands are of reference types, assignment copies the reference to the object.

This is called value assignment: the value is assigned.


Post a Comment

0 Comments