Logical Operators कैसे काम करते हैं
Logical operators true-or-false सवालों को combine करते हैं, जैसे 'क्या मेरे पास मेरा ticket है AND क्या gate खुला है?'। C# कई conditions से decisions लेने के लिए and, or, और not उपयोग करता है।
In this page:
Syntax
bool result = condition1 && condition2; // AND
bool result = condition1 || condition2; // OR
bool result = !condition; // NOT
Logical Operators
Logical operators boolean expressions को combine करते हैं: && (AND), || (OR), और ! (NOT)। && और || short-circuit करते हैं — अगर left side पहले ही result तय कर दे, तो right side कभी evaluate नहीं होता। यह तब मायने रखता है जब right side के side effects हों।
Note:
Short-circuiting आपको बिना null-check exception के
if (obj != null && obj.IsValid) सुरक्षित रूप से लिखने देता है।उदाहरण: Logical Operators
using System;
class Program
{
static void Main(string[] args)
{
bool isLoggedIn = true;
bool isAdmin = false;
Console.WriteLine(isLoggedIn && isAdmin);
Console.WriteLine(isLoggedIn || isAdmin);
Console.WriteLine(!isAdmin);
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
&&और||के बजाय&और|का उपयोग करना, जो short-circuit नहीं करते और हमेशा दोनों sides evaluate करते हैं।x == 1 || 2लिखना, जो compile नहीं होता क्योंकि2एक bool नहीं है।&&के right side में किसी method call पर भरोसा करना कि यह हमेशा चलेगा जब short-circuiting इसे skip कर सकता है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: