From the course: Advanced C#: Functional Programming Patterns
Unlock this course with a free trial
Join today to access over 24,500 courses taught by industry experts.
Higher-order: LINQ - C# Tutorial
From the course: Advanced C#: Functional Programming Patterns
Higher-order: LINQ
The real power of functional programming comes from combining delegates and lambda expressions with higher-order functions. In .NET, a great example of this is LINQ, a library that allows you to query data collections using the IEnumerable< T > interface. If you want to learn how to write functional code in C#, studying LINQ is an excellent place to start. The code we're looking at in this example is in this method called LinqExamples, and I start out by creating a range of numbers. So this is an IEnumerable< T >, where T is an integer. Some of the LINQ methods accept a delegate as an argument, which allows us to specify the logic we want the function to execute, and that's what we're looking at on line 17 and line 21. So somebody at Microsoft created the method. Its purpose is to return the last element of a sequence. There are two versions of this method. One takes no arguments and that uses the default. It'll pick the last element that would be 120. But then I can override that…