Šajā rakstā mēs uzzināsim visu par dažāda veida operatoriem C # programmēšanas valodā un to lietošanu.
Operatori ir simboli, kas tiek izmantoti operandu darbību veikšanai. Operandi var būt mainīgie un / vai konstantes.
Piemēram , ir 2+3
, +
ir operators, kas tiek izmantots, lai veiktu saskaitīšanu darbību, bet 2
un 3
ir operandiem.
Operatorus izmanto, lai manipulētu ar mainīgajiem un vērtībām programmā. C # atbalsta vairākus operatorus, kas tiek klasificēti pēc to veikto darbību veida.
1. Pamata uzdevuma operators
Pamata piešķiršanas operators (=) tiek izmantots, lai piešķirtu vērtības mainīgajiem. Piemēram,
dubultā x; x = 50,05;
Šeit x tiek piešķirts 50,05.
1. piemērs: pamata uzdevuma operators
using System; namespace Operator ( class AssignmentOperator ( public static void Main(string() args) ( int firstNumber, secondNumber; // Assigning a constant to variable firstNumber = 10; Console.WriteLine("First Number = (0)", firstNumber); // Assigning a variable to another variable secondNumber = firstNumber; Console.WriteLine("Second Number = (0)", secondNumber); ) ) )
Kad mēs palaidīsim programmu, izeja būs:
Pirmais skaitlis = 10 Otrais skaitlis = 10
Šis ir vienkāršs piemērs, kas parāda piešķiršanas operatora izmantošanu.
Jūs, iespējams, pamanījāt ( )
piemērā izmantot cirtainās iekavas . Mēs tos apspriedīsim virkņu formatējumā. Pagaidām vienkārši paturiet prātā, ka (0)
to aizstāj ar pirmo mainīgo, kas seko virknei, (1)
aizstāj ar otro mainīgo utt.
2. Aritmētiskie operatori
Aritmētiskos operatorus izmanto, lai veiktu tādas aritmētiskās darbības kā saskaitīšana, atņemšana, reizināšana, dalīšana utt.
Piemēram,
int x = 5; int y = 10; int z = x + y; // z = 15C # Aritmētiskie operatori
Operators | Operatora nosaukums | Piemērs |
---|---|---|
+ | Papildinājumu operators | 6 + 3 vērtē līdz 9 |
- | Atņemšanas operators | 10 - 6 vērtē līdz 4 |
* | Reizināšanas operators | 4 * 2 vērtē līdz 8 |
/ | Divīzijas operators | 10/5 vērtē līdz 2 |
% | Modulo operators (atlikušais) | 16% 3 vērtē līdz 1 |
2. piemērs: Aritmētiskie operatori
using System; namespace Operator ( class ArithmeticOperator ( public static void Main(string() args) ( double firstNumber = 14.40, secondNumber = 4.60, result; int num1 = 26, num2 = 4, rem; // Addition operator result = firstNumber + secondNumber; Console.WriteLine("(0) + (1) = (2)", firstNumber, secondNumber, result); // Subtraction operator result = firstNumber - secondNumber; Console.WriteLine("(0) - (1) = (2)", firstNumber, secondNumber, result); // Multiplication operator result = firstNumber * secondNumber; Console.WriteLine("(0) * (1) = (2)", firstNumber, secondNumber, result); // Division operator result = firstNumber / secondNumber; Console.WriteLine("(0) / (1) = (2)", firstNumber, secondNumber, result); // Modulo operator rem = num1 % num2; Console.WriteLine("(0) % (1) = (2)", num1, num2, rem); ) ) )
Kad mēs palaidīsim programmu, izeja būs:
14,4 + 4,6 = 19 14,4 - 4,6 = 9,8 14,4 * 4,6 = 66,24 14,4 / 4,6 = 3,1304347826087 26% 4 = 2
Aritmētiskās darbības tiek veiktas iepriekš minētajā piemērā. Mainīgos lielumos var aizstāt ar konstantēm. Piemēram,
rezultāts = 4,5 + 2,7; // rezultātam būs 7.2 rezultāts = firstNumber - 3.2; // rezultāts turēs 11.2
3. Relāciju operatori
Relāciju operatori tiek izmantoti, lai pārbaudītu divu operandu attiecības. Ja attiecības ir patiesas, rezultāts būs true
, pretējā gadījumā tas notiks false
.
Relāciju operatori tiek izmantoti lēmumu pieņemšanā un ciklos.
C # Relāciju operatoriOperators | Operatora nosaukums | Piemērs |
---|---|---|
== | Vienāds ar | 6 == 4 vērtē kā nepatiesu |
> | Varenāks, kā | 3> -1 vērtē kā patiesu |
< | Mazāk nekā | 5 <3 vērtē kā nepatiesu |
> = | Lielāks vai vienāds ar | 4> = 4 vērtē kā patiesu |
<= | Mazāks par vai vienāds ar | 5 <= 3 vērtē kā nepatiesu |
! = | Nav vienāds ar | 10! = 2 novērtē patiesību |
3. piemērs: Relāciju operatori
using System; namespace Operator ( class RelationalOperator ( public static void Main(string() args) ( bool result; int firstNumber = 10, secondNumber = 20; result = (firstNumber==secondNumber); Console.WriteLine("(0) == (1) returns (2)",firstNumber, secondNumber, result); result = (firstNumber> secondNumber); Console.WriteLine("(0)> (1) returns (2)",firstNumber, secondNumber, result); result = (firstNumber < secondNumber); Console.WriteLine("(0) = secondNumber); Console.WriteLine("(0)>= (1) returns (2)",firstNumber, secondNumber, result); result = (firstNumber <= secondNumber); Console.WriteLine("(0) <= (1) returns (2)",firstNumber, secondNumber, result); result = (firstNumber != secondNumber); Console.WriteLine("(0) != (1) returns (2)",firstNumber, secondNumber, result); ) ) )
Kad mēs palaidīsim programmu, izeja būs:
10 == 20 atgriež False 10> 20 atgriež False 10 = 20 atgriež False 10 <= 20 atgriež True 10! = 20 atgriež True
4. Loģiskie operatori
Loģiskie operatori tiek izmantoti, lai veiktu loģisku darbību, piemēram, and
, or
. Loģiskie operatori darbojas ar būla izteiksmēm ( true
un false
) un atgriež būla vērtības. Loģiskie operatori tiek izmantoti lēmumu pieņemšanā un ciklos.
Lūk, kā rezultāts tiek vērtēts loģiskajiem AND
un OR
operatoriem.
Operands 1 | Operands 2 | VAI (||) | UN (&&) |
---|---|---|---|
taisnība | taisnība | taisnība | taisnība |
taisnība | nepatiesa | taisnība | nepatiesa |
nepatiesa | taisnība | taisnība | nepatiesa |
nepatiesa | nepatiesa | nepatiesa | nepatiesa |
Vienkāršos vārdos tabulu var apkopot šādi:
- Ja viens no operandiem ir patiess,
OR
operators to novērtēstrue
. - Ja kāds no operanda ir viltus,
AND
operators to novērtēsfalse
.
4. piemērs: loģiskie operatori
using System; namespace Operator ( class LogicalOperator ( public static void Main(string() args) ( bool result; int firstNumber = 10, secondNumber = 20; // OR operator result = (firstNumber == secondNumber) || (firstNumber> 5); Console.WriteLine(result); // AND operator result = (firstNumber == secondNumber) && (firstNumber> 5); Console.WriteLine(result); ) ) )
Kad mēs palaidīsim programmu, izeja būs:
Patiesa Nepatiesa
5. Unāri operatori
Atšķirībā no citiem operatoriem, vienotie operatori darbojas vienā operandā.
C # unāri operatoriOperators | Operatora nosaukums | Apraksts |
---|---|---|
+ | Unary Plus | Atstāj operanda zīmi tādu, kāda tā ir |
- | Unārijs Mīnuss | Apgriež operanda zīmi |
++ | Pieaugums | Pieauguma vērtība par 1 |
- | Samazinājums | Samazināšanas vērtība par 1 |
! | Loģiskas negācijas (nav) | Apgriež būla vērtību |
5. piemērs: Nepareizi operatori
using System; namespace Operator ( class UnaryOperator ( public static void Main(string() args) ( int number = 10, result; bool flag = true; result = +number; Console.WriteLine("+number = " + result); result = -number; Console.WriteLine("-number = " + result); result = ++number; Console.WriteLine("++number = " + result); result = --number; Console.WriteLine("--number = " + result); Console.WriteLine("!flag = " + (!flag)); ) ) )
Kad mēs palaidīsim programmu, izeja būs:
+ skaitlis = 10 -numurs = -10 ++ skaitlis = 11 - skaitlis = 10! karogs = aplams
Pieauguma (++)
un samazināšanas (--)
operatorus var izmantot kā prefiksu un pēcfiksu. Ja to izmanto kā prefiksu, mainīgā lieluma vērtības izmaiņas ir redzamas tajā pašā rindā, un, ja to izmanto kā postfiksu, mainīgā vērtības izmaiņas tiek rādītas nākamajā rindā. Tas būs skaidrs ar piemēru zemāk.
6. piemērs: operatori Post un Pre Increment C #
using System; namespace Operator ( class UnaryOperator ( public static void Main(string() args) ( int number = 10; Console.WriteLine((number++)); Console.WriteLine((number)); Console.WriteLine((++number)); Console.WriteLine((number)); ) ) )
Kad mēs palaidīsim programmu, izeja būs:
10 11 12 12
We can see the effect of using ++
as prefix and postfix. When ++
is used after the operand, the value is first evaluated and then it is incremented by 1
. Hence the statement
Console.WriteLine((number++));
prints 10
instead of 11
. After the value is printed, the value of number is incremented by 1
.
The process is opposite when ++
is used as prefix. The value is incremented before printing. Hence the statement
Console.WriteLine((++number));
prints 12
.
The case is same for decrement operator (--)
.
6. Ternary Operator
The ternary operator ? :
operates on three operands. It is a shorthand for if-then-else
statement. Ternary operator can be used as follows:
variable = Condition? Expression1 : Expression2;
Trīskāršais operators darbojas šādi:
- Ja nosacījuma norādītā izteiksme ir
true
, izteiksmes1 rezultāts tiek piešķirts mainīgajam. - Ja tā ir
false
, izteiksmes 2 rezultāts tiek piešķirts mainīgajam.
7. piemērs: trīskāršais operators
using System; namespace Operator ( class TernaryOperator ( public static void Main(string() args) ( int number = 10; string result; result = (number % 2 == 0)? "Even Number" : "Odd Number"; Console.WriteLine("(0) is (1)", number, result); ) ) )
Kad mēs palaidīsim programmu, izeja būs:
10 ir pāra skaitlis
Lai uzzinātu vairāk, apmeklējiet C # trīskāršo operatoru.
7. Operatori Bitwise un Bit Shift
Bitu manipulācijas darbību veikšanai tiek izmantoti bitu kustības un bitu nobīdes operatori.
C # Bitwise un Bit Shift operatoriOperators | Operatora nosaukums |
---|---|
~ | Bitwise papildinājums |
& | Bitu virzienā UN |
| | Bitwise OR |
^ | Bitwise ekskluzīvs VAI |
<< | Pa kreisi Shift |
>> | Pa labi pa labi |
8. piemērs: Operators Bitwise un Bit Shift
using System; namespace Operator ( class BitOperator ( public static void Main(string() args) ( int firstNumber = 10; int secondNumber = 20; int result; result = ~firstNumber; Console.WriteLine("~(0) = (1)", firstNumber, result); result = firstNumber & secondNumber; Console.WriteLine("(0) & (1) = (2)", firstNumber,secondNumber, result); result = firstNumber | secondNumber; Console.WriteLine("(0) | (1) = (2)", firstNumber,secondNumber, result); result = firstNumber secondNumber; Console.WriteLine("(0) (1) = (2)", firstNumber,secondNumber, result); result = firstNumber << 2; Console.WriteLine("(0) <> 2; Console.WriteLine("(0)>> 2 = (1)", firstNumber, result); ) ) )
Kad mēs palaidīsim programmu, izeja būs:
~ 10 = -11 10 & 20 = 0 10 | 20 = 30 10 20 = 30 10 <> 2 = 2
Lai uzzinātu vairāk, apmeklējiet operatoru C # Bitwise un Bit Shift.
8. Salikto uzdevumu operatori
C # salikto uzdevumu operatoriOperators | Operatora nosaukums | Piemērs | Līdzvērtīgs |
---|---|---|---|
+ = | Papildinājuma piešķiršana | x += 5 | x = x + 5 |
- = | Atņemšanas uzdevums | x -= 5 | x = x - 5 |
* = | Reizināšanas uzdevums | x *= 5 | x = x * 5 |
/ = | Divīzijas uzdevums | x /= 5 | x = x / 5 |
% = | Modulo uzdevums | x %= 5 | x = x % 5 |
& = | Bitwise UN uzdevums | x &= 5 | x = x & 5 |
| = | Bitwise OR Piešķiršana | x |= 5 | x = x | 5 |
= | Bitor XOR piešķiršana | x ^= 5 | x = x 5 |
<< = | Kreisās maiņas uzdevums | x <<= 5 | x = x << 5 |
>> = | Labās maiņas uzdevums | x>>= 5 | x = x>> 5 |
=> | Lambda operators | x => x*x | Returns x*x |
9. piemērs: Savienojuma piešķiršanas operators
using System; namespace Operator ( class BitOperator ( public static void Main(string() args) ( int number = 10; number += 5; Console.WriteLine(number); number -= 3; Console.WriteLine(number); number *= 2; Console.WriteLine(number); number /= 3; Console.WriteLine(number); number %= 3; Console.WriteLine(number); number &= 10; Console.WriteLine(number); number |= 14; Console.WriteLine(number); number ^= 12; Console.WriteLine(number); number <>= 3; Console.WriteLine(number); ) ) )
Kad mēs palaidīsim programmu, izeja būs:
15 12 24 8 2 2 14 2 8 1
Mēs vēlāk apspriedīsim par Lambda operatoriem.