Book Image

C# Programming Cookbook

By : Dirk Strauss
Book Image

C# Programming Cookbook

By: Dirk Strauss

Overview of this book

During your application development workflow, there is always a moment when you need to get out of a tight spot. Through a recipe-based approach, this book will help you overcome common programming problems and get your applications ready to face the modern world. We start with C# 6, giving you hands-on experience with the new language features. Next, we work through the tasks that you perform on a daily basis such as working with strings, generics, and lots more. Gradually, we move on to more advanced topics such as the concept of object-oriented programming, asynchronous programming, reactive extensions, and code contracts. You will learn responsive high performance programming in C# and how to create applications with Azure. Next, we will review the choices available when choosing a source control solution. At the end of the book, we will show you how to create secure and robust code, and will help you ramp up your skills when using the new version of C# 6 and Visual Studio
Table of Contents (21 chapters)
C# Programming Cookbook
Credits
About the Author
Acknowledgements
About the Reviewer
www.PacktPub.com
Preface
Index

Events versus observables


Being developers, we should all be quite familiar with events. Most developers have been creating events since we started writing code. In fact, if you have even dropped a button control on a form and double-clicked the button to create the method that handles the click of the button, you have created an event. In .NET, we can declare events using the event keyword, publish to the event by invoking it, and subscribe to that event by adding a handler to the event. We therefore have the following operations:

  • Declare

  • Publish

  • Subscribe

With Rx, we have a similar structure where we declare a data stream, publish data to that stream, and subscribe to it.

Getting ready

First, we will see how an event works in C#. We will then see the working of an event using Rx and, in doing so, highlight the differences.

How to do it…

  1. In your console application, add a new class called DotNet. To this class, add a property called AvailableDatatype:

    public class DotNet
    {
        public string  AvailableDatatype...