← Back to C# Course | Chapter 12: Delegates & Events | Lesson 4 of 6

Func, Action, Predicate की जानकारी

Func, Action, और Predicate तैयार-बने delegate types हैं: Func एक जवाब वापस देता है, Action बस कुछ करता है, और Predicate हां या नहीं में जवाब देता है। वे आपको अपना खुद का बनाने से बचाते हैं।

In this page:

  1. Func, Action, Predicate
Syntax
csharp
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

markup
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));
    }
}
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. #}
आम गलतियां
  1. Func उपयोग करना जब method कुछ return नहीं करता, जबकि Action सही type है।
  2. यह भूल जाना कि Func का आखिरी type argument return type है।
  3. Predicate<T> उपयोग करना और इसके bool के अलावा कुछ और return करने की उम्मीद करना।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.