C Sharp Operators and Expressions

Revision as of 19:03, 14 January 2008 by Neil (Talk | contribs) (New page: In C# Variables and Constants we looked at using variables and constants in C# and also described the different variable and constant types. Being able ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Revision as of 19:03, 14 January 2008 by Neil (Talk | contribs) (New page: In C# Variables and Constants we looked at using variables and constants in C# and also described the different variable and constant types. Being able ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In C# Variables and Constants we looked at using variables and constants in C# and also described the different variable and constant types. Being able to create constants and variables is only part of the story however. The next step is to learn how use these variables and constants in our C# code. The primary method for working with data in constants and variables is in the form of expressions.

What is an Expression?

The most basic expression consists of an operator, two operands and an assignment. The following is an example of an expression:

int theResult = 1 + 2;

In the above example the (+) operator is used to add two operands (1 and 2) together. The assignment operator (=) subsequently assigns the result of the addition to an integer variable named the Result. The operands could just have easily been variables or constants (or a mixture of each) instead of the actually numerical values used in the example.

In the remainder of this chapter we will look at the various types of operators available in C#.

The Basic Assignment Operator

We have already looked at the most basic of assignment operators, the = operator. This assignment operator simply assigns the result of an expression to a variable. In essence the = assignment operator takes two operands. The left hand operand is the variable to which a value is to be assigned and the right hand operand is the value ot be assigned. The right hand operand is, more often than not an expression which performs some type of arithmetic or logical evaluation. The following examples are all valid uses of the assignment operator:

x = 10;  // Assigns the value 10 to a variable named x

x = y + z; // Assigns the result of variable y added to variable z to variable x;

x = y;   // Assigns the value of variable y to variable x

Assignment operators may also be chained to assign the same value to multiple variables. For example, the following code example assigns the value 20 to the x, y and z variables:

int x, y, z;

x = y = z = 20;

Compound Assignment Operators

C# provides a number of operators designed to combine an assignment with a mathematical or logical operation. These are primarily of use when performing an evaluation where the result is to be stored in one of the operands. For example, one might write an expression as follows:

x = x + y;

The above expression adds the value contained in variable x to the value contained in variable y and stores the result in variable x. This can be simplified using the addition compound assignment operator:

x += y

The above expression performs exactly the same task as x = x + y but saves the programmer some typing. This is yet another feature that C# has inherited from the C programming language.

Numerous compound assignment operators are available in C#. The most frequently used are outlined in the following table:

OperatorDescription
x += yAdd x to y and place result in x
x -= y>td>Subtract y from x and place result in x
x *= yMultiply x by y and place result in x
x /= yDivide x by y and place result in x
x %= yPerform Modulo on x and y and place result in x
x &= yAssign to x the result of logical AND operation on x and y
x |= yAssign to x the result of logical OR operation on x and y
x ^= yAssign to x the result of logical Exclusive OR on x and y