#1,151 – The Func Delegate Type

The .NET Framework includes the Func predefined delegate type, which represents the signature of a method that returns some type and accepts zero or more parameters.

Overloads of Func are :

  • delegate TResult Func<out TResult>()
  • delegate TResult Func<in T, out TResult>(T p1)
  • delegate TResult Func<in T1, in T2, out TResutl>(T1 p1, T2 p2)
  • etc…

You might define your own delegate type:

        public delegate T Merger<T>(T thing1, T thing2);

        static void MergeSomething<T>(Merger<T> someMerger, T thing1, T thing2)
        {
            T result = someMerger(thing1, thing2);
        }

However, you could instead use the Func delegate type, avoiding the definition of your own custom delegate type.

        static void MergeSomething2<T>(Func<T, T, T> someMerger, T thing1, T thing2)
        {
            T result = someMerger(thing1, thing2);
        }

Func<T,T,T> indicates that we’ll pass in a function that takes two parameters of a given type and returns a value of the same type.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #1,151 – The Func Delegate Type

  1. Bhavik says:

    very simple and easy to understand this code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: