C# Operators

Introduction to C# Operators

किसी भी C# program का मुख्य उद्देश्य कोई operation perform करना होता है। Operations data के साथ perform किये जाता है। Data आपके program में variables के रूप में available रहता है।

Data के अलावा program में operations perform करने के लिए आपको operators की भी आवश्यकता होती है। Operators special symbols या elements होते है जो operations perform करने के लिए variables के साथ प्रयोग किये जाते है।

उदाहरण के लिए निचे दिए गए code को देखिये। इस code में दो variables a और b create किये गए है जिनमें क्रमशः 7 और 3 value store की गयी है।

int a = 7;
int b = 3;

मान लीजिये आप इन दोनों variables को add करना चाहते है या इन दोनों variables के साथ addition operation perform करना चाहते है तो इसके लिए आपको इन दोनों variables के साथ addition operator (+) को use करना होगा। जैसा की निचे दिए code में किया गया है।

int c;
c = a + b;

जिन variables को operators के साथ define किया जाता है वे operands कहलाते है। ऊपर दिए गए code में में a, b और c operands है। इन्हें = और + operators के साथ define किया गया है।

जब भी कोई operation perform होता है तो वह result produce करता है। उस result को किसी variable में store करने की आवश्यकता होती है जैसा की ऊपर दिए गए code में c variable create करके उसमें a + b operation का result store किया गया है।

Operators दो तरह के होते है।

  • Unary – ऐसे operators जो सिर्फ एक ही operand (variable) के साथ प्रयोग किये जाते है unary operators कहलाते है।
  • Binary – ऐसे operators जो दो operands के साथ प्रयोग किये जाते है binary operators कहलाते है।

C# आपको operators को modify (overload) करके उन्हें user defined types (class, struct आदि) के साथ प्रयोग करने का mechanism भी provide करती है जिसे operator overloading कहा जाता है। इसके बारे में अधिक जानकारी के लिए आप सम्बंधित tuotrial पढ़ सकते है।

C# में available विभिन्न operators के बारे में आगे detail से बताया जा रहा है। इन्हें precedence के आधार पर categories किया गया है।

Primary Operators

जब operators और operands को एक साथ define किया जाता है वह statement expression कहलाता है। किसी भी expression में primary operators की precedence (प्राथमिकता) सबसे अधिक होती है।

यानी की यदि expression में एक से अधिक operators को use किया गया है तो सबसे पहले primary operators को evaluate किया जाता है। C# में available प्रमुख primary operators के बारे में निचे बताया जा रहा है।

Dot (.) Operator

Dot operator को member access operator भी कहा जाता है इसे मुख्यतः class और struct आदि types के members को access करने के लिए प्रयोग किया जाता है।

obj.member-name;

Null Conditional Operator (?.)

Member access perform करने से पहले NULL value के लिए test करने के लिए null condition operator का प्रयोग किया जाता है।

int num = myClass?.Num;  //num will be NULL if myclass.Num is NULL

new Operator

C# में new operator type instantiation के लिए प्रयोग किया जाता है। इसे user define reference types (class, struct आदि ) के objects create करने के लिए प्रयोग किया जाता है।

myclass obj = new myClass();

typeof() Operator

यह operator किसी भी type का object प्राप्त करने के लिए use किया जाता है। Argument के रूप में आप कोई भी user defined या built in type pass करते है। यह operator pass किये type का object return करता है जिसे System.Type class के object में store किया जाता है।

System.Type type = typeof(myClass);

sizeof() Operator

यह operator pass किये गए type की size bytes में return करता है।

int myClassSize = sizeof(myClass);

Pointer Dereferencing Operator ->

C# में pointer dereferencing operator किसी user defined type के members को pointer variable द्वारा access करने के लिए प्रयोग किया जाता है।

pt->num = 10;  // Num is member of any user defined type

Additive Operators

C# में निचे दिए गए additive operators available है।

Addition (+)

यह एक binary operator है जो variables के बीच addition perform करने के लिए प्रयोग किया जाता है।

c = a + b; //Adds values of a & b then store result in c

Subtraction (-)

यह एक binary operator है। प्रयोग किये जाने पर यह left side के variable की value में से right side के variable की value को subtract करता है और remaining value को result के रूप में produce करता है।

c = a – b //Subtract value of b from a then stores result in c

Multiplicative Operators

C# में निचे दिए जा रहे multiplicative operators available है।

Multiplication (*)

इस operator द्वारा variables के बीच multiplication perform किया जाता है।

c = a * b; //Multiply values of a & b stores result in c

Division (/)

इस operator द्वारा variables के बीच division perform किया जाता है। यह operator left side के variable की value को right side के variable की value से divide करता है और result produce करता है।

c = a / b;  //Divide a by b then store result in c

Modulus (%)

इस operator द्वारा variables के बीच division perform किया जाता है। यह operator division के बाद बची हुई शेष value को result के रूप में produce करता है।

c = a % b;  // Divide a by b then store remaining value in c

Type Testing Operators

Type testing operators किसी type को test करने के लिए प्रयोग किये जाते है। C# में available type testing operators के बारे में निचे दिया जा रहा है।

is

C# में is operator यह test करने के लिए प्रयोग किया जाता है की क्या left side के type को right side के type में cast किया जा सकता है। यदि cast संभव होती है तो यह operator true produce करता है नहीं तो false produce करता है।

if(a is b)
{
     // do something here
}

as

यह operator right side के type को left side के type में cast करता है।

myType a = x as y;

Relational Operators

Relational Operators का प्रयोग variables को compare करने के लिए किया जाता है। इन्हें decision making के लिए भी प्रयोग किया जाता है। Relational operators result के रूप में boolean (TRUE, FALSE) value return करते है। C# में available relational operators की list निचे दी जा रही है।

OperatorExplanation Syntax
> (Greater than)Checks whether a is greater than ba > b
< (Lesser than)Checks whether a is lesser than ba < b
>= (Greater than & equal)Checks whether a is greater than & equal to b a >= b
<= (Lesser than & equal)Checks whether a is lesser than & equal toa <= b

Shift Operators

Shift operators variables पर bit level operations perform करने के लिए प्रयोग किये जाते है। मुख्यतः shift operators bits को left या right में shift करते है। Low level programming के लिए ये operators useful होते है। C# में available shift operators के बारे में निचे बताया जा रहा है।

Left Shift («)

यह operator left side में दिए गए variable की bits को right side में दिए गए number जितना left में shift करता है।

int c = a << 2;  //Shift bits of a by 2 to left side and store result in c;

Right Shift (»)

यह operator left side में दिए गए variable की bits को right side में दिए गए number जितना right shift करता है।

int c a >> 2;  //shift bits of a by 2 to right side and store result in c

Equality Operators

Equality operators यह check करने के लिए प्रयोग किये जाते है की कोई दो variables समान है या असमान है। C# में available quality operators के बारे में निचे बताया जा रहा है।

Equal To (==)

यह operator check करता है की दोनों variables समान है या नहीं। यदि दोनों variables समान होते है तो true return किया जाता है। यदि दोनों variables असमान होते है तो false return किया जाता है।

if(a == b)
{
       do something here…
}

Not Equal To (!=)

यह variable check करता है की define किये गए दोनों variables असमान है या नहीं। यदि दोनों variable असमान होते है तो true return किया जाता है। यदि दोनों variables समान होते है तो false return किया जाता है।

if(a!=b)
{
     do something here…
}

Logical Operators

Logical Operators द्वारा program में logic perform किया जाता है। Logical operators द्वारा operations bit level पर perform किये जाते है। C# में available logical operators के बारे में निचे बताया जा रहा है।

OperatorExplanation Syntax
&& (AND)returns true if both conditions are truecond1 && cond2
|| (OR)returns true if any of the two condition is truecond1 || cond2
! (NOT)returns true if condition is not true, Unary operator !cond

Assignment Operators

Program में assignment operations perform करने के लिए assignment operators use किये जाते है। C# में available assignment operators के बारे में निचे बताया जा रहा है।

OperatorExplanation Syntax
= (Assignment)Assign value of a to ba = b
+= (Plus and assignment)Add value of a & b then assign result to aa += b
-= (Minus and assignment)Subtract value of b from a then assign result to aa -= b
*= (Multiply and assignment)Multiply a & b then assign result to aa *= b
/= (Divide and assignement)Divide a by b then assign result to aa /= b
%= (Modulus and assignment)Divide a by b then assign remaining value to aa %= b

Conditional Operator (?:)

C# आपको conditional operator provide करती है। यह operator ? और : के combination से बनता है। यह operator if else statement का short रूप होता है। इस operator का general syntax निचे दिया जा रहा है।

condition ? true-part : false-part

जैसा की आप ऊपर दिए गए synatx में देख सकते है सबसे पूर्व condition define की जाती है। इसके बाद question mark (?) symbol लगाया जाता है। इस symbol के बाद वह statement लिखा जाता है जो condition true होने पर use किया जाएगा। इसके बाद colon (:) symbol लगाया जाता है। Colon symbol के बाद वह code लिखा जाता है जो condition के true होने पर use किया जाएगा।

इस operator को मुख्यतः condition के आधार पर variables को value assign करने के लिए प्रयोग किया जाता है।

Increment/Decrement Operators

Variables की values को increase और decrease करने के लिए increment और decrement operators का प्रयोग किया जाता है। C# में available increment/decrement operators के बारे में निचे दिया जा रहा है।

Postfix Increment (var++)

यह operator variable की value को return करता है। इसके बाद उस value को एक number से increase करके storage location को update करता है।

Prefix Increment (++var)

यह operator पहले variable की value को एक number से increase करता है और storage location को update करता है। इसके बाद उस variable की value को return करता है।

Postfix Decrement (var–)

यह operator variable की value को return करता है और storage location को update करता है। इसके बाद उस variable की value को 1 number से decrease करके storage location पर update करता है।

Prefix Decrement (–var)

यह operator variable की value को पहले एक number से decrease करता है और storage location पर update करता है इसके बाद उस value को return करता है।