An operator is a symbol (e.g., =, +, >, etc.) that causes C# to take an action. That action might be an assignment of a value to a variable, the addition of two values, or a comparison of two values, etc.
The single equal sign (=) is used to assign a value to a variable, in this case the value 15 to the variable myVariable:
count = 15;
More sophisticated operators, such as the greater-than comparison operator (>) used to compare two values:
if ( valueOne > valueTwo )
The preceding if statement compares valueOne with valueTwo; if the former is larger than the latter, the test evaluates true, and the if statement executes.
Assignment Operator (=) :
Simple Arithmetical Operators (+, -, *, / ) :
When you divide two integers, C# divides like a child in the fourth grade: it throws away any fractional remainder. Thus, dividing 17 by 4 returns a value of 4 (17/4 = 4, with C# discarding the remainder of 1).
This limitation is specific to integer division. If you do not want the fractional part thrown away, you can use one of the types that support decimal values, such as float and double. Division between two floats (using the / operator) returns a fractional answer.
The modulus operator turns out to be more useful than you might at first imagine. When you perform modulus n on a number that is a multiple of n, the result is zero. Thus 80%10=0 because 80 is an even multiple of 10.
Increment and Decrement Operators :
Calculate and Reassign Operators:
The need to perform this kind of manipulation is so common that C# includes special operators for self-assignment. Among these operators are +=, -=, *=, /=, and %=, which, respectively, combine addition, subtraction, multiplication, division, and modulus, with self-assignment.
Increment or Decrement by 1:
Thus, if you want to increment the variable myAge by 1 you can write:
The Prefix and Postfix Operators:
resultingValue = originalValue++;
The question arises: do you want to assign before you increment the value or after? In other words, if originalValue starts out with the value 10, do you want to end with both resultingValue and originalValue equal to 11, or do you want resultingValue to be equal to 10 (the original value) and originalValue to be equal to 11?
C# offer two specialized ways to use the increment and decrement operators: prefix and postfix. The way you use the ++ operator determines the order in which the increment/decrement and assignment take place. The semantics of the prefix increment operator is "increment the original value, and then assign the incremented value to result" while the semantics of the postfix increment operator is "assign the original value to result, and then increment original."
To use the prefix operator to increment, place the ++ symbol before the variable name; to use the postfix operator to increment, place the ++ symbol after the variable name.Relational Operators :
Relational operators compare two values and then return a Boolean value (true or false). The greater-than operator (>), for example, returns true if the value on the left of the operator is greater than the value on the right. Thus, 5>2 returns the value true, while 2>5 returns the value false.
Not Equals !=
Greater than or Equal to >=
Less than < class="docText">Each of these relational operators acts as you might expect. Notice that most of these operators are composed of two characters. For example, the greater than or equal to operator (>=) is created with the greater than symbol (>) and the equal sign (=). Notice also that the equals operator is created with two equal signs (==) because the single equal sign alone (=) is reserved for the assignment operator.
The C# equals operator (==) tests for equality between the objects on either side of the operator. This operator evaluates to a Boolean value (true or false). Thus, the statement:
myX == 5;
evaluates to true if and only if the myX variable has a value of 5.
Use of Logical Operators with Conditionals :
If statements test whether a condition is true. Often you will want to test whether two conditions are both true, only one is true, or neither is true. C# provides a set of logical operators for this.
The Conditional Operator :
cond-expr ? expression1 : expression2
This operator evaluates a conditional expression (an expression that returns a value of type bool) and then invokes either expression1 if the value returned from the conditional expression is true, or expression2 if the value returned is false. The logic is: "if this is true, do the first; otherwise do the second.
Short-Circuit Evaluation :
Within the outer parentheses are two expressions, (x == 8) and (y == 12), which are separated by an or operator (||). Because x is 8, the first term (x == 8) evaluates true. There is no need to evaluate the second term (y == 12).Again, there is no need to evaluate the second term. Because the first term is false, the and must fail. (Remember, for an and statement to evaluate true, both tested expressions must evaluate true.)
In cases such as these, the C# compiler will short-circuit the evaluation; the second test will never be performed.
Operator Precedence :
The rules of precedence tell the compiler which operators to evaluate first. As is the case in algebra, multiplication has higher precedence than addition, so 5+7*3 is equal to 26 rather than 36. Both addition and multiplication have higher precedence than assignment, so the compiler will do the math and then assign the result (26) to myVariable only after the math is completed.
VISUAL STUDIO INTRODUCTION
C SHARP INTRODUCTION
C SHARP OUT LOOK
DOT NET AND C SHARP
C SHARP APPLICATION STRICTURE
OOPS AND C SHARP
IDE AND C SHARP
INSTANTIATING OBJECTS IN C SHARP
CLASSES AND OBJECTS IN C SHARP
OPERATORS IN C SHARP
SWITCH AND ITERATION IN C SHARP
BRANCHING IN C SHARP
CONSTANTS AND STRING
STATIC AND INSTANCE MEMBERS IN DOT NET