Irfan
Irfan

Reputation: 2771

Function as an Argument in .NET

Why do we need to pass a function as an argument to another function. I was wondering why dont we simply call the function in the function where we pass it?

Is is because accessibility of a argumented-function is limited in the called function? Can someone please explain, what happens in background when a function is passed as argument and do we really need it in .NET like frameworks?

Upvotes: 1

Views: 86

Answers (4)

slfan
slfan

Reputation: 9129

Functions can be variables and therefore be calculated at runtime. You declare a variable and assign a function. Later on, you can call this function, which can even change at runtime.

.NET languages treat them as a first class citizens since the first version. Delegates (C# keyword delegate) are object-oriented function pointers. You can use function pointers in the Strategy pattern or in all types of callback functions. .NET events (keyword event) implement the Observer pattern and are used for all Windows events.

Lambda expressions (syntax =>) which are often used in combination with LINQ are only a special form of delegates. The compiler translates them into anonymous delegates which have been around since .NET 2.0.

Upvotes: 1

Mr.Pe
Mr.Pe

Reputation: 739

The best example is http://en.wikipedia.org/wiki/Strategy_pattern

As used by a lot of Linq methods.

( new List<int>() { 02, 15, 54, 61, 53, 84, 12 } ).All( i => { return i > 20; } );

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 148980

The ability to pass a function to a function or return a function from a function—i.e. higher-order functions—are the basis of an entire programming paradigm, known as functional programming. One key example in .NET is Linq, where you can selectively manipulate a collection based on user defined functions.

For example, the Where function allows you to specify how you want to filter the elements of a collection. You can do this with a single line of code:

var results = collection.Where(x => x.SomeProperty == someValue);

Whereas if you wanted to do this without a function argument, you'd need a separate function every time you'd like to filter, map, reduce a collection, etc. Given that simple manipulation of a collection is a very common task, this would result in much more code in a typical project.

There are a number of other benefits as well, such as the ability to specify a function to call without actually calling it—also known as lazy evaluation. This is actually how Linq works internally. The above line won't filter the results until you try to enumerate the result set. This can lead to significant performance improvements when used properly.

Upvotes: 4

TGH
TGH

Reputation: 39248

The advantage is that you can inject behavior into the method by calling the method with different methods as parameters. It's the same way you can call a regular method with different integer values. If you call it from within the function it's static and can't change. However if you take the method as a parameter you can pass whatever method you like as long as its return type and parameters meet the requirements.

Upvotes: 3

Related Questions