Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Functional C#
Functional C#

Functional C#: Uncover the secrets of functional programming using C# and change the way you approach your applications

Arrow left icon
Profile Icon Wisnu Anggoro
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (3 Ratings)
Paperback Dec 2016 370 pages 1st Edition
eBook
₱579.99 ₱2245.99
Paperback
₱2806.99
Subscription
Free Trial
Arrow left icon
Profile Icon Wisnu Anggoro
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (3 Ratings)
Paperback Dec 2016 370 pages 1st Edition
eBook
₱579.99 ₱2245.99
Paperback
₱2806.99
Subscription
Free Trial
eBook
₱579.99 ₱2245.99
Paperback
₱2806.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Functional C#

Chapter 2. Walkthrough Delegates

In the previous chapter, we applied delegates in the code we created. When we discussed the concept of functional programming, we applied one of the built-in delegates that C# has. In this chapter, we are going to delve into the delegates that will be used a lot in functional C# programming by discussing the following topics:

  • The definition, syntax, and use of delegates
  • Combining delegates into multicast delegates
  • Using built-in delegates
  • Understanding the variance in delegates

Introducing delegates

A delegate is a data type in C# that encapsulates a method that has particular parameters and return types (signatures). In other words, a delegate will define the parameters and the return type of a method. Delegates are similar to function pointers in C/C++ since both stores the reference to the method with a particular signature. Like a function pointer in C/C++, a delegate keeps a memory address of the method it refers to. The compiler will complain if it refers to a function with a different signature. However, because of the unmanaged nature of the C++ language, one can point functions to arbitrary locations (by casting).

Let's take a look at the following delegate syntax:

[AccessModifier] delegate ReturnType DelegateName([parameters]); 

Here is the explanation for each element of the preceding delegate syntax:

  • AccessModifier: This is the modifier that is used to set the accessibility of the delegate. It can be public, private, internal, or protected. However...

Built-in delegates

In C#, not only are we able to declare a delegate, but we are also able to use the built-in delegate from the C# standard library. This built-in delegate also applies to the generic data type, so let's discuss the generic delegate prior to discussing the built-in delegate.

Generic delegates

A delegate type can use a generic type as its parameter. Using the generic type, we can put off the specification of one or more types in parameters or return values until the delegate is initialized into a variable. In other words, we do not specify the data types of the delegate's parameters and return values when we define a delegate type. To discuss this in more detail, let's take a look at the following code, which we can find at GenericDelegates.csproj:

public partial class Program 
{ 
  private delegate T FormulaDelegate<T>(T a, T b); 
} 

We have a delegate name, FormulaDelegate, using the generic data type. As we can see, there is a T symbol, which represents...

Distinguishing variance in delegates

A generic delegate has the ability to be assigned by a method that has an unmatched signature to the delegate. We can call this variance in delegates. There are two variances in delegates, and they are covariance and contravariance. Covariance allows a method to have a return type that is more derived (subtype) than the return type that is defined in the delegate. On the other hand, contravariance allows a method to have parameter types that are less derived (supertype) than the parameter types that are defined in the delegate.

Covariance

The following is an example of covariance in delegates, which we can find in the Covariance.csproj project. First, we initialize the following delegate:

public partial class Program 
{ 
  private delegate TextWriter CovarianceDelegate(); 
} 

We now have a delegate returning the TextWriter data type. Then, we also create the StreamWriterMethod() method returning the StreamWriter object, which has the following implementation...

Summary

A delegate is useful in order to encapsulate a method. It is like any data type in C# in which a variable can be initialized to have the delegate data type. Since it similar to data types, increment and decrement operations can be applied to the delegate, making it possible to create a multicast delegate from several delegates. However, one thing to remember, since the Delegate.Combine() and Delegate.Remove() methods return the Delegate data type, is that we have to cast the return of both methods to the expected instance delegate when using them. Compared to the += and -= operators use, however, since they are implemented at the language level in the compiler and the delegate type is known, there's no need to cast the result of the increment and decrement delegate operation.

C# also has built-in delegates, Action and Func, which make the code shorter, and the definition of the delegate becomes easier and quicker. As a result, the code gets simpler to be analyzed. Also, there...

Left arrow icon Right arrow icon

Key benefits

  • This book focuses on the functional paradigm of C#, which will give you a whole new angle on coding with C#
  • It illustrates the advantages that functional programming brings to the table and the associated coding benefits
  • This practical guide covers all the aspects of functional programming and provides solutions that can be applied in business scenarios

Description

Functional programming makes your application faster, improves performance, and increases your productivity. C# code is written at a higher level of abstraction, so that code will be closer to business requirements, abstracting away many low-level implementation details. This book bridges the language gap for C# developers by showing you how to create and consume functional constructs in C#. We also bridge the domain gap by showing how functional constructs can be applied in business scenarios. We’ll take you through lambda expressions and extension methods, and help you develop a deep understanding of the concepts and practices of LINQ and recursion in C#. By the end of the book, you will be able to write code using the best approach and will be able to perform unit testing in functional programming, changing how you write your applications and revolutionizing your projects.

Who is this book for?

This book is suitable for C# developers with basic prior knowledge of C# and with no functional programming experience at all.

What you will learn

  • Develop an application using the functional approach
  • Implement unit testing to functionally program code
  • Create efficient code using functional programming
  • Work through a LINQ query so you can work with data
  • Compose asynchronous programs to create a responsive application
  • Use recursion in function programming in order to simplify code
  • Optimize the program code using Laziness and Caching Techniques

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 30, 2016
Length: 370 pages
Edition : 1st
Language : English
ISBN-13 : 9781785282225
Vendor :
Microsoft
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Dec 30, 2016
Length: 370 pages
Edition : 1st
Language : English
ISBN-13 : 9781785282225
Vendor :
Microsoft
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 8,114.97
Functional C#
₱2806.99
Mastering C# and .NET Framework
₱2500.99
C# Programming Cookbook
₱2806.99
Total 8,114.97 Stars icon
Banner background image

Table of Contents

11 Chapters
1. Tasting Functional Style in C# Chevron down icon Chevron up icon
2. Walkthrough Delegates Chevron down icon Chevron up icon
3. Expressing Anonymous Methods with Lambda Expressions Chevron down icon Chevron up icon
4. Extending Object Functionality with Extension Methods Chevron down icon Chevron up icon
5. Querying Any Collection Easily with LINQ Chevron down icon Chevron up icon
6. Enhancing the Responsiveness of the Functional Program with Asynchronous Programming Chevron down icon Chevron up icon
7. Learning Recursion Chevron down icon Chevron up icon
8. Optimizing the Code using Laziness and Caching Techniques Chevron down icon Chevron up icon
9. Working with Pattern Chevron down icon Chevron up icon
10. Taking an Action in C# Functional Programming Chevron down icon Chevron up icon
11. Coding Best Practice and Testing the Functional Code Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(3 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Albertazzi Angelo Apr 23, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Il libro contiene ottimi esempi di programmazione funzionale, applicabili alla programmazione di tutti i giorni. Buono anche come "ripasso" per delegate, metodi anonimi e lambda.
Amazon Verified review Amazon
Jaime Secondo Gibertoni Pestana Jan 11, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent introduction to functional programming with C#
Amazon Verified review Amazon
窪田 昌二 May 12, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
すごく分かりやすいです。関数型プログラミングをc#で実践するために必要なことがすべて書いてあります。いままで曖昧だったことがすごく明確になりました。ありがとうございます。
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.