C# TUTORIAL
C# Operators
Let us explore more about the operators types in C Sharp.T
Results in C# are computed by building expressions. These expressions are built by combining variables and operators together into statements. . The following table describes the operators, their precedence, and associativity.
Table 2-4. Operators with their precedence and Associativity
Category (by precedence) | Operator(s) | Associativity |
---|---|---|
Primary | (x) x.y f(x) a[x] x++ x-- new typeof sizeof checked unchecked | left |
Unary | + - ! ~ ++x --x (T)x | left |
Multiplicative | * / % | left |
Additive | + - | left |
Shift | << >> | left |
Relational | < > <= >= is | left |
Equality | == != | right |
Logical AND | & | left |
Logical XOR | ^ | left |
Logical OR | | | left |
Conditional AND | && | left |
Conditional OR | || | left |
Ternary | ?: | right |
Assignment | = *= /= %= += -= <<= >>= &= ^= |= | right |
Left associativity means that operations are evaluated from left to right. Right associativity mean all operations occur from right to left, such as assignment operators where everything to the right is evaluated before the result is placed into the variable on the left.
Most operators are either unary or binary. Unary operators form expressions on a single variable, but binary operators form expressions with two variables. We will first try to understand the familiar binary operators. Take a look at the example below.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int x, y, result; float floatresult; x = 7; y = 5; result = x+y; Console.WriteLine("x+y: {0}", result); result = x-y; Console.WriteLine("x-y: {0}", result); result = x*y; Console.WriteLine("x*y: {0}", result); result = x/y; Console.WriteLine("x/y: {0}", result); floatresult = (float)x/(float)y; Console.WriteLine("x/y: {0}", floatresult); result = x%y; Console.WriteLine("x%y: {0}", result); result += x; Console.WriteLine("result+=x: {0}", result); Console.ReadLine(); } } } |
If you run the above code, you should see the output something similar to the below.
x+y: 12 x-y: 2 x*y: 35 x/y: 1 x/y: 1.4 x%y: 2 result+=x: 9 |
Let us now take a look at at the unitary operators.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int x = 0; int preIncrement; int preDecrement; int postIncrement; int postDecrement; int positive; int negative; sbyte bitNot; bool logNot; preIncrement = ++x; Console.WriteLine("pre-Increment: {0}", preIncrement); preDecrement = --x; Console.WriteLine("pre-Decrement: {0}", preDecrement); postDecrement = x--; Console.WriteLine("Post-Decrement: {0}", postDecrement); postIncrement = x++; Console.WriteLine("Post-Increment: {0}", postIncrement); Console.WriteLine("Final Value of x: {0}", x); positive = -postIncrement; Console.WriteLine("Positive: {0}", positive); negative = +postIncrement; Console.WriteLine("Negative: {0}", negative); bitNot = 0; bitNot = (sbyte)(~bitNot); Console.WriteLine("Bitwise Not: {0}", bitNot); logNot = false; logNot = !logNot; Console.WriteLine("Logical Not: {0}", logNot); Console.ReadLine(); } } } |
If you run the above program you will see the output similar to the following
pre-Increment: 1 pre-Decrement: 0 Post-Decrement: 0 Post-Increment: -1 Final Value of Unary: 0 Positive: 1 Negative: -1 Bitwise Not: -1 Logical Not: True |