Operators
Anoop K. C.
anoop@baabte.com
www.facebook.com/anoopb
aabte
twitter.com/anoop_baabte
in.linkedin.com/in/anoopbaa
...
In C#, an operator is a program element that is applied to one or more operands in
an expression or statement.
Operators t...
C# Operators - Classification
Assignment operator - =
Arithmetic operators - +, -, * , /, %
Comparison operators - ==, !=,...
Assignment operator
Assignment operator is used to assign a value to a variable, that is it assigns the
value on the right...
Arithmetic operators
Arithmetic operators are used to perform arithmetic operations
+ - Add
- - Subtract
* - Multiply
/ - ...
Comparison operators
Comparison operators are used to compare the expression on the left hand side
(LHS) with the expressi...
Conditional operators
Conditional operators are used to perform operations based on simultaneous
evaluation of multiple ex...
Ternary Operator (?:)
Ternary Operator is used to Return one value if the condition is true, or another
value if it is fal...
Null Coalescing operator (??)
A null coalescing operator returns the left-hand operand if the operand is not null;
otherwi...
Evaluation and operator precedence
In an expression that contains multiple operators, the order in which the operators
are...
Associativity
When two or more operators that have the same precedence are present in an
expression, they are evaluated ba...
Adding paranthesis
We can change the order imposed by operator precedence and associativity by
using parentheses.
For exam...
Operator overloading
We can change the behavior of operators for custom classes and structs. This
process is referred to a...
Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ s...
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, N...
C# operators
Upcoming SlideShare
Loading in …5
×

C# operators

433 views

Published on

This slide is about operators in C# programming language

Published in: Software, Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
433
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C# operators

  1. 1. Operators Anoop K. C. anoop@baabte.com www.facebook.com/anoopb aabte twitter.com/anoop_baabte in.linkedin.com/in/anoopbaa bte/ +91 9746854752
  2. 2. In C#, an operator is a program element that is applied to one or more operands in an expression or statement. Operators that take one operand, such as the increment operator (++) or new, are referred to as unary operators. Operators that take two operands, such as arithmetic operators (+,- ,*,/), are referred to as binary operators. One operator, the conditional operator (?:), takes three operands and is the sole ternary operator in C#.
  3. 3. C# Operators - Classification Assignment operator - = Arithmetic operators - +, -, * , /, % Comparison operators - ==, !=, >, >=, <, <= Conditional operators - &&, || Ternary operator - ?: Null coalescing operator- ??
  4. 4. Assignment operator Assignment operator is used to assign a value to a variable, that is it assigns the value on the right hand side to the variable on the left hand side Example: Int a = 10;
  5. 5. Arithmetic operators Arithmetic operators are used to perform arithmetic operations + - Add - - Subtract * - Multiply / - Division % - Modulus (divides the value on the left side by the value on the right side and outputs the remainder ) Example: 9 % 2 = 1
  6. 6. Comparison operators Comparison operators are used to compare the expression on the left hand side (LHS) with the expression on the right hand side (RHS) and it outputs a boolean value as the result == - equal to != - not equal to > - Greater than >= - Greater than or equal to < - Less than <= - Less than or equal to
  7. 7. Conditional operators Conditional operators are used to perform operations based on simultaneous evaluation of multiple expressions && - and Example: If (a == b && b != c) [if a equal to b and b not equal to c – this condition will evaluate to true if and only if (a == b) is true and (b != c) is true] || - or Example: If (a == b || b != c) [if a equal to b or b not equal to c – this condition will evaluate to true if either of (a == b) is true or (b != c) is true]
  8. 8. Ternary Operator (?:) Ternary Operator is used to Return one value if the condition is true, or another value if it is false. It is some sort of if-else code in a short form. Example:
  9. 9. Null Coalescing operator (??) A null coalescing operator returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand. Example: x ?? y , means if x is non-null, evaluate to x; otherwise, y. In the syntax x ?? Y x is the first operand which is a variable of a nullable type. y is the second operand which has a non-nullable value of the same type. While executing code, if x evaluates to null, y is returned as the value The null coalescing operator (??) is right-associative which means it is evaluated from right to left. So if you have an expression of the form x ?? y ?? z, this expression is evaluated as x?? (y?? z)
  10. 10. Evaluation and operator precedence In an expression that contains multiple operators, the order in which the operators are applied is determined by operator precedence, associativity, and parentheses. Each operator has a defined precedence. In an expression that contains multiple operators that have different precedence levels, the precedence of the operators determines the order in which the operators are evaluated. For example, the following statement assigns 3 to n1. n1 = 11 - 2 * 4; The multiplication is executed first because multiplication takes precedence over subtraction.
  11. 11. Associativity When two or more operators that have the same precedence are present in an expression, they are evaluated based on associativity. Left-associative operators are evaluated in order from left to right. For example, x * y / z is evaluated as (x * y) / z. Right-associative operators are evaluated in order from right to left. The assignment operator is right associative. For exmple: The assignment operators and the ternary operator (?:) are right associative. All other binary operators are left associative.
  12. 12. Adding paranthesis We can change the order imposed by operator precedence and associativity by using parentheses. For example, 2 + 3 * 2 ordinarily evaluates to 8, because multiplicative operators take precedence over additive operators. However, if we write the expression as (2 + 3) * 2, the addition is evaluated before the multiplication, and the result is 10
  13. 13. Operator overloading We can change the behavior of operators for custom classes and structs. This process is referred to as operator overloading To overload an operator on a custom class requires creating a method on the class with the correct signature. The method must be named "operator X" where X is the name or symbol of the operator being overloaded. Unary operators have one parameter, and binary operators have two parameters. In each case, one parameter must be the same type as the class or struct that declares the operator, as demonstrated in the following example: public static Complex operator + (Complex c1, Complex c2)
  14. 14. Follow us @ twitter.com/baabtra Like us @ facebook.com/baabtra Subscribe to us @ youtube.com/baabtra Become a follower @ slideshare.net/BaabtraMentoringPartner Connect to us @ in.linkedin.com/in/baabtra Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  15. 15. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Cafit Square, Hilite Business Park, Near Pantheerankavu, Kozhikode Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com

×