Func, Action and lambda

Description

lambda expression are often used with Func and Action delegates.

Func and Action are predefined delegates with generic parameters.


delegate TResult Func <out TResult>  ();
delegate TResult Func <in T, out TResult>  (T arg);
delegate TResult Func <in T1, in T2, out TResult>  (T1 arg1, T2 arg2);
/* w w  w . jav a 2s.  c  o  m*/
... and so on, up to T16

delegate void Action  ();
delegate void Action <in T>  (T arg);
delegate void Action <in T1, in T2>  (T1 arg1, T2 arg2);

... and so on, up to T16

Example

Here is an example of how use to use Func to create a lambda expression.


using System;/*from  www  .j  av  a  2  s .  c  o  m*/


class Program
{
    public static void Main()
    {

        Func<int, int> sqr = x => x * x;

        Console.WriteLine(sqr(3));
    }
}

The output:





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor