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

eBook
£7.99 £32.99
Paperback
£41.99
Subscription
Free Trial
Renews at £16.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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 : 9781785881039
Vendor :
Microsoft
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
£16.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
£169.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 £5 each
Feature tick icon Exclusive print discounts
£234.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 £5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total £ 120.97
Functional C#
£41.99
Mastering C# and .NET Framework
£36.99
C# Programming Cookbook
£41.99
Total £ 120.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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.