Func, Action, Predicate की जानकारी
Func, Action, और Predicate तैयार-बने delegate types हैं: Func एक जवाब वापस देता है, Action बस कुछ करता है, और Predicate हां या नहीं में जवाब देता है। वे आपको अपना खुद का बनाने से बचाते हैं।
In this page:
Syntax
Func<type1, type2, ReturnType> func = (a, b) => expression; // returns a value
Action<type> action = value => statement; // returns nothing
Predicate<type> predicate = value => condition; // returns bool
Func, Action, Predicate
Func<T,...,TResult> एक ऐसे method को represent करता है जो एक value return करता है, Action<T,...> एक ऐसे को represent करता है जो कुछ return नहीं करता, और Predicate<T> एक ऐसे को represent करता है जो एक T लेता है और bool return करता है।
ये built-in generic delegate types आम shapes के लिए एक custom delegate type declare करने से बचाते हैं। ये LINQ और collection methods में भारी उपयोग होते हैं।
Note:
Func<int, int, int> का मतलब है "दो ints लेता है, एक int return करता है" — आखिरी type parameter हमेशा return type होता है।उदाहरण: Func, Action, Predicate
using System;
class Program
{
static void Main(string[] args)
{
Func<int, int, int> add = (a, b) => a + b;
Action<string> print = msg => Console.WriteLine(msg);
Predicate<int> isEven = n => n % 2 == 0;
Console.WriteLine(add(4, 5));
print("Hello from Action");
Console.WriteLine(isEven(10));
}
}
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. #}
आम गलतियां
Funcउपयोग करना जब method कुछ return नहीं करता, जबकिActionसही type है।- यह भूल जाना कि
Funcका आखिरी type argument return type है। Predicate<T>उपयोग करना और इसके bool के अलावा कुछ और return करने की उम्मीद करना।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: