Lambda Expressions क्या हैं
एक lambda एक छोटा नामहीन function है जो तुरंत लिखा जाता है, जैसे instructions की एक quick sticky note। यह किसी list को filter करने जैसे छोटे कामों के लिए सुविधाजनक है।
In this page:
Syntax
(parameters) => expression
(parameters) => { statements; return value; }
DelegateName variable = x => x * 2;
Lambda Expressions
एक lambda expression => syntax उपयोग करते हुए एक compact, inline, नामहीन function है, जहां भी एक delegate की उम्मीद हो वहां उपयोगी। (x, y) => x + y एक पूरे method के बराबर है लेकिन inline लिखा गया।
Lambdas LINQ, event handlers, और Func/Action के साथ भारी उपयोग होते हैं।
Note:
single expression body वाले lambda को कोई
return या braces नहीं चाहिए — expression की value अपने आप return होती है।उदाहरण: Lambda Expressions
using System;
delegate int Transform(int x);
class Program
{
static void Main(string[] args)
{
Transform square = x => x * x;
Transform increment = x => x + 1;
Console.WriteLine(square(5));
Console.WriteLine(increment(5));
}
}
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. #}
आम गलतियां
- किसी lambda में एक loop variable capture करना और बाद में इसकी value से हैरान होना।
- delegate type के लिए गलत parameters वाला एक lambda लिखना, जो एक compile error है।
- किसी lambda को लंबा और जटिल बनाना, जब एक named method ज़्यादा साफ़ होता।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: