C# Operators Tutorial | Learn Arithmetic, Comparison, Logical Concepts
Last updated on 16th Aug 2022, Blog, Tutorials
C# Operators Introduction :-
Administrators in C# square measure Special Symbols to Denote the Operation That the Program must Perform. This Tutorial Explains C# Operators thoroughly With Examples:
In our past tutorial exercise, we tend to observed with regards to Conditional Statements in C#. We tend to in addition find out a way to utilize if, if-else and if-else if explanations to characterize varied conditions.A contingent assertion, for instance, “if” is otherwise known as a dynamic assertion as they provide the consumer a element to characterize a end in read of a selection characterized by directors.The directors provide a technique for characterizing decisions visible of Logic, Arithmetic Operations, Comparison, so forth An administrator is a picture that advises the compiler to perform specific numerical or legitimate controls. C# has made arrangement of underlying directors and provides the related kind of kind of
- Math Operators
- Social Operators
- Coherent Operators
- Bitwise Operators
- Task Operators
- Misc Operators
Math Operators :-
Following table shows each one of the mathematics directors upheld by C#. Expect variable A holds ten and variable B holds twenty then, at that time, −
Operator | Description Example |
---|---|
+ | Adds 2 operands A + B = thirty |
– | Subtracts second quantity from the first A – B = -10 |
* | Multiplies each operands A * B = two hundred |
/ | Divides dividend by de-numerator B / A = a pair of |
% | Modulus Operator associated remainder of once an number division B you are a = zero |
++ | Increment operator will increase number worth by one A++ = eleven |
— | Decrement operator decreases number worth by one A– = nine |
Social Operators :-
Following table shows each one of the social directors upheld by C#. settle for variable A holds ten and variable B holds twenty, then, at that time, −
Operator | Description Example |
---|---|
== | Checks if the values of 2 operands are equal or not, if affirmative then the condition becomes true. (A == B) isn’t true. |
!= | Checks if the values of 2 operands are equal or not, if values don’t seem to be equal then condition becomes true. (A != B) is true. |
> | Checks if the worth of the left quantity is bigger than the worth of the right quantity, if affirmative then the condition becomes true. (A > B) isn’t true. |
< | Checks if the worth of the left quantity is a smaller amount than the worth of the right quantity, if affirmative then the condition becomes true. (A < B) is true. |
>= | Checks if the worth of left quantity is bigger than or adequate to the worth of right quantity, if affirmative then condition becomes true. (A >= B) isn’t true.2 = 15,that is 0000 1111 |
Task Operators :-
There are following task directors upheld by C# −
Operator | Description Example |
---|---|
= | Simple assignment operator, Assigns worths from right facet operands to left facet operand C = A + B assigns value of A + B into C |
+= | Add AND assignment operator, It adds right quantity to the left quantity and assign the result to left operand C += A is cherish C = C + A |
-= | Subtract AND assignment operator, It subtracts right quantity from the left quantity and assign the result to left operand C -= A is cherish C = C – A |
*= | Multiply AND assignment operator, It multiplies right quantity with the left quantity and assign the result to left operand C *= A is cherish C = C * A |
/= | Divide AND assignment operator, It divides left quantity with the correct quantity and assign the result to left operand C /= A is cherish C = C / A |
%= | Modulus AND assignment operator, It takes modulus mistreatment 2 operands and assign the result to left operand folk music= A is cherish C = C nada A> 2 |
&= | Bitwise AND assignment operator C &= two is same as C = C & two |
^= | bitwise exclusive OR and assignment operator C ^= two is same as C = C ^ two |
|= | bitwise comprehensive OR and assignment operator C |= two is same as C = C | two |
Various Operators :-
There don’t seem to be several alternative vital directors together with sizeof, typeof and ? : upheld by C#.
Operator | Description Example |
---|---|
sizeof() | Returns the dimensions of an information sort. sizeof(int), returns 4. |
typeof() | Returns the kind of a category. typeof(StreamReader);& Returns the address of the associate variable. |
Administrator Precedence in C# :-
Administrator priority decides the gathering of terms in associate articulation. This influences assessment of associate articulation. bound directors have higher priority than others; for example, the rise administrator has higher priority than the growth administrator.For instance x = seven + three * 2; here, x is allotted thirteen, not twenty since administrator * has higher priority than +, therefore the primary assessment happens for 3*2 and later on seven is value-added into it.
Here, directors with the foremost noteworthy priority show up at the best purpose of the table, those with the smallest amount show up at the bottom. Within associate articulation, higher priority directors are assessed 1st. Show Examples
Category | Operator Associativity |
---|---|
Postfix | () [] -> . ++ – – Left to right |
Unary | + – ! ~ ++ – – (type)* & sizeof Right to left |
Multiplicative | * / % Left to right |
Additive | + – Left to right |
Shift | > Left to right |
Relational | < >= Left to right |
Equality | == != Left to right |
Bitwise | AND & Left to right |
Bitwise | XOR ^ Left to right |
Bitwise | OR | Left to right |
Logical | AND && Left to right |
Logical | OR || Left to right |
Conditional | ?: Right to left |
Assignment | = += -= *= /= %=>>= |
C# Comparison Operators :
Examination directors square measure used to research 2 qualities:
Operator Name | Example |
---|---|
== | Equivalent to x == y |
!= | Not equal x != y |
> | More noteworthy than x > y |
< | Less than x < y |
>= | More distinguished than or equivalent to x >= y |
C# Logical Operators :
Legitimate directors square measure used to make a decision the principle between factors or qualities:
Operator Name | DescriptionExample |
---|---|
&& | Intelligent Returns valid assumptive the 2 assertions square measure true x < five && x < ten |
|| | Intelligent Returns valid on the off likelihood that one in all the assertions is true x < five || x < four |
! | wise not Reverse the end result, returns bastard within the event that the end result is true !(x < five && x < 10) |
An operator may be an image that tells the compiler to perform specific mathematical or logical manipulations. C# has wealthy set of integral operators and provides the subsequent variety of variety of
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
This tutorial explains the arithmetic, relational, logical, bitwise, assignment, and alternative operators one by one.
Arithmetic Operators:
Assume variable A holds ten and variable B holds twenty then −
Show Examples:
Operator | Description Example |
---|---|
+ | Adds 2 operands A + B = thirty |
– | Subtracts second quantity from the first A – B = -10 |
* | Multiplies each operands A * B = two hundred |
/ | Divides dividend by de-numerator B / A = two |
% | Modulus Operator ANd remainder of when an number division B you are a = zero |
++ | Increment operator will increase number price by one A++ = eleven |
— | Decrement operator decreases number price by one A– = nine |
Relational Operators
Following table shows all the relative operators supported by C#. Assume variable A holds ten and variable B holds twenty, then −
Show Examples:
Operator | Description Example |
---|---|
== | Checks if the values of 2 operands are unit equal or not, if affirmative then the condition becomes true. (A == B) isn’t true. |
!= | Checks if the values of 2 operands are unit equal or not, if values don’t seem to be equal then the condition becomes true. (A != B) is true. |
> | Checks if the worth of the left quantity is larger than the worth of the right quantity, if affirmative then the condition becomes true. (A > B) isn’t true. |
< | Checks if the worth of left quantity is a smaller amount than the worth of right quantity, if affirmative then condition becomes true. (A < B) is true. |
>= | Checks if the worth of left quantity is larger than or adequate the worth of right quantity, if affirmative then condition becomes true. (A >= B) isn’t true. |
Left to right
- Relational < >= Left to right
- Equality == != Left to right
- Bitwise AND & Left to right
- Bitwise XOR ^ Left to right
- Bitwise OR | Left to right
- Logical AND && Left to right
- Logical OR || Left to right
- Conditional ?: Right to left
- Assignment = += -= *= /= %=>>=
Addition: The ‘+’ operator adds 2 operands. as an example, x+y.
Subtraction: The ‘-‘ operator subtracts 2 operands. as an example, x-y.
Multiplication: The ‘*’ operator multiplies 2 operands. as an example, x*y.
Division: The ‘/’ operator divides the primary quantity by the second. as an example, x/y.
Modulus: The ‘%’ operator returns the rest once the initial quantity is split by the second. as an example, x%y.
The ones falling into the class of single Operators are:
Increment: The ‘++’ operator is employed to increment the worth of associate degree number. Once placed before the variable name (also known as pre-increment operator), its price is incremented instantly. as an example, ++x. And once it’s placed when the variable name (also known as post-increment operator), its price is preserved quickly till the execution of this statement and it gets updated before the execution of the ensuing statement. as an example, x + +.
Decrement: The ‘–‘ operator is employed to decrement the worth of associate degree number. Once placed before the variable name (also known as pre-decrement operator), its price is decremented instantly. as an example, –x.And once it’s placed when the variable name (also known as post-decrement operator), its price is preserved quickly till the execution of this statement and it gets updated before the execution of the ensuing statement. as an example, x–.
Conclusion :-
In this tutorial exercise, we have a tendency to seen with regards to the varied types of directors within the C# programming language. we have a tendency to seen with regards to the use and pictures of those directors.The number-crunching administrator is used by the program to perform basic pure mathematics tasks like enlargement, deduction, duplication, division, and so onSocial directors square measure those that square measure used to approve a association between the 2 operands like they’re equivalent, additional distinguished than, not specifically, so on Task directors square measure used for authorisation esteems to a variable.A most elementary illustration of the task administrator is “equivalent to”. Coherent directors square measure used for activity wise activities like AND, OR, NOT, so onAdministrators square measure widely used for saying conditions in dynamic proclamations, whereas utilizing circles or whereas activity mathematical tasks.
Are you looking training with Right Jobs?
Contact Us- Windows Azure Interview Questions and Answers
- Salesforce Architecture Tutorial
- Wrapper Class in Salesforce Tutorial
- salesforce lightning
Related Articles
Popular Courses
- VM Ware Training
11025 Learners
- Microsoft Dynamics Training
12022 Learners
- Siebel Training
11141 Learners
- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know