C Sharp TutorialC Sharp .NET Compiler Platform (Roslyn)C Sharp 3.0 FeaturesC Sharp 4.0 FeaturesC Sharp 5.0 FeaturesC Sharp 6.0 FeaturesC Sharp 7.0 FeaturesC Sharp Access ModifiersC Sharp Access network shared folder with username and passwordC Sharp Accessing DatabasesC Sharp Action FiltersC Sharp Aliases of built-in typesC Sharp an overview of collectionsC Sharp Anonymous typesC Sharp ArraysC Sharp ASP.NET IdentityC Sharp AssemblyInfo.cs ExamplesC Sharp Async-AwaitC Sharp Async/await, Backgroundworker, Task and Thread ExamplesC Sharp Asynchronous SocketC Sharp AttributesC Sharp Authentication handlerC Sharp BackgroundWorkerC Sharp BigIntegerC Sharp Binary SerializationC Sharp BindingListC Sharp Built-in TypesC Sharp CachingC Sharp CastingC Sharp Checked and UncheckedC Sharp CLSCompliantAttributeC Sharp Code ContractsC Sharp Code Contracts and AssertionsC Sharp Collection InitializersC Sharp Comments and regionsC Sharp Common String OperationsC Sharp Conditional StatementsC Sharp Constructors and FinalizersC Sharp Creating Own MessageBox in Windows Form ApplicationC Sharp Creational Design PatternsC Sharp Cryptography (System.Security.Cryptography)C Sharp Data AnnotationC Sharp DateTime MethodsC Sharp DelegatesC Sharp Dependency InjectionC Sharp DiagnosticsC Sharp Dynamic typeC Sharp EnumC Sharp Equality OperatorC Sharp Equals and GetHashCodeC Sharp EventsC Sharp Exception HandlingC Sharp Expression TreesC Sharp Extension MethodsC Sharp File and Stream I/OC Sharp FileSystemWatcherC Sharp Func delegatesC Sharp Function with multiple return valuesC Sharp Functional ProgrammingC Sharp Garbage Collector in .NETC Sharp Generating Random NumbersC Sharp Generic Lambda Query BuilderC Sharp GenericsC Sharp Getting Started: Json with C SharpC Sharp GuidC Sharp Handling FormatException when converting string to other typesC Sharp Hash FunctionsC Sharp ICloneableC Sharp IComparableC Sharp IDisposable interfaceC Sharp IEnumerableC Sharp ILGeneratorC Sharp ImmutabilityC Sharp Implementing Decorator Design PatternC Sharp Implementing Flyweight Design PatternC Sharp Import Google ContactsC Sharp Including Font ResourcesC Sharp IndexerC Sharp InheritanceC Sharp Initializing PropertiesC Sharp INotifyPropertyChanged interfaceC Sharp InterfacesC Sharp InteroperabilityC Sharp IQueryable interfaceC Sharp IteratorsC Sharp KeywordsC Sharp Lambda expressionsC Sharp Lambda ExpressionsC Sharp LINQ QueriesC Sharp LINQ to Objects

C Sharp Func delegates

From WikiOD

Syntax[edit | edit source]

  • public delegate TResult Func<in T, out TResult>(T arg)
  • public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2)
  • public delegate TResult Func<in T1, in T2, in T3, out TResult>(T1 arg1, T2 arg2, T3 arg3)
  • public delegate TResult Func<in T1, in T2, in T3, in T4, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)

Parameters[edit | edit source]

Parameter Details
arg or arg1 the (first) parameter of the method
arg2 the second parameter of the method
arg3 the third parameter of the method
arg4 the fourth parameter of the method
T or T1 the type of the (first) parameter of the method
T2 the type of the second parameter of the method
T3 the type of the third parameter of the method
T4 the type of the fourth parameter of the method
TResult the return type of the method

Without parameters[edit | edit source]

This example shows how to create a delegate that encapsulates the method that returns the current time

static DateTime UTCNow()
    return DateTime.UtcNow;

static DateTime LocalNow()
    return DateTime.Now;

static void Main(string[] args)
    Func<DateTime> method = UTCNow;
    // method points to the UTCNow method
    // that retuns current UTC time  
    DateTime utcNow = method();

    method = LocalNow;
    // now method points to the LocalNow method
    // that returns local time

    DateTime localNow = method();

With multiple variables[edit | edit source]

static int Sum(int a, int b)
    return a + b;

static int Multiplication(int a, int b)
    return a * b;

static void Main(string[] args)
    Func<int, int, int> method = Sum;
    // method points to the Sum method
    // that retuns 1 int variable and takes 2 int variables  
    int sum = method(1, 1);

    method = Multiplication;
    // now method points to the Multiplication method

    int multiplication = method(1, 1);

Lambda & anonymous methods[edit | edit source]

An anonymous method can be assigned wherever a delegate is expected:

Func<int, int> square = delegate (int x) { return x * x; }

Lambda expressions can be used to express the same thing:

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

In either case, we can now invoke the method stored inside square like this:

var sq = square.Invoke(2);

Or as a shorthand:

var sq = square(2);

Notice that for the assignment to be type-safe, the parameter types and return type of the anonymous method must match those of the delegate type:

Func<int, int> sum = delegate (int x, int y) { return x + y; } // error
Func<int, int> sum = (x, y) => x + y; // error

Covariant & Contravariant Type Parameters[edit | edit source]

Func also supports Covariant & Contravariant

// Simple hierarchy of classes.
public class Person { }
public class Employee : Person { }

class Program
    static Employee FindByTitle(String title)
        // This is a stub for a method that returns
        // an employee that has the specified title.
        return new Employee();

    static void Test()
        // Create an instance of the delegate without using variance.
        Func<String, Employee> findEmployee = FindByTitle;

        // The delegate expects a method to return Person,
        // but you can assign it a method that returns Employee.
        Func<String, Person> findPerson = FindByTitle;

        // You can also assign a delegate 
        // that returns a more derived type 
        // to a delegate that returns a less derived type.
        findPerson = findEmployee;