Book Image

Learn C# in 7 days

By : Gaurav Aroraa
1 (1)
Book Image

Learn C# in 7 days

1 (1)
By: Gaurav Aroraa

Overview of this book

This book takes a unique approach to teach C# to absolute beginners. You’ll learn the basics of the language in seven days. It takes a practical approach to explain the important concepts that build the foundation of the C# programming language. The book begins by teaching you the basic fundamentals using real-world practical examples and gets you acquainted with C# programming. We cover some important features and nuances of the language in a hands-on way, helping you grasp the concepts in a fluid manner. Later, you’ll explore the concepts of Object-Oriented Programming (OOP) through a real-world example. Then we dive into advanced-level concepts such as generics and collections, and you’ll get acquainted with objects and LINQ. Towards the end, you’ll build an application that covers all the concepts explained in the book. By the end of this book, you will have next-level skills and a good knowledge of the fundamentals of C#.
Table of Contents (15 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Pattern matching


In a general way, pattern matching is a way to compare contents in predefined formats in an expression. The format is nothing but a combination of different matches.

In C# 7.0, pattern matching is a feature. With the use of this feature, we can implement method dispatch on properties other than the type of an object.

Pattern matching supports various expressions; let's discuss these with code-examples.

Note

Patterns can be constant patterns: Type patterns or Var patterns.

is expression

The is expression enables the inspection of an object and its properties and determines whether it satisfies the pattern:

public static string MatchingPatterUsingIs(object character) 
{ 
   if (character is null) 
   return $"{nameof(character)} is null. "; 
   if (character is char) 
   { 
      var isVowel = IsVowel((char) character) ? "is a
      vowel" : "is a consonent"; 
      return $"{character} is char and {isVowel}. "; 
   } 
   if (character is string) 
   { 
      var chars = ((string...