TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Pattern Matching

C# Pattern Matching for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

C# Pattern Matching

C# pattern matching is a feature that allows us to perform matching on data or any object. We can perform pattern matching using the is expression and switch statement.

is expression is used to check, whether an object is compatible with given type or not.

In the following example, we are implementing is expression.

C# Pattern Matching using is Example

using System;
namespace CSharpFeatures
{
    class Student
    {
        public string Name { get; set; } = "Rahul kumar";
    }
    class PatternMatchingExample
    {
        public static void Main(string[] args)
        {
            Student student = new Student();
            if(student is Student)
            {
                Console.WriteLine(student.Name);
            }
        }
    }
}

Output:

Rahul kumar

We can also perform pattern matching in switch statement. See, the following example.

C# Pattern in Switch Case Example 2

using System;
namespace CSharpFeatures
{
    class Student
    {
        public string Name { get; set; } = "Rahul kumar";
    }
    class Teacher
    {
        public string Name { get; set; } = "Peter";
        public string Specialization { get; set; } = "Computer Science";
    }
    class PatternMatchingExample
    {
        public static void Main(string[] args)
        {
            Student student = new Student();
            Teacher teacher = new Teacher();
            PatterInSwitch(student);
            PatterInSwitch(teacher);
        }
        public static void PatterInSwitch(object obj)
        {
            switch (obj)
            {
                case Student student:
                    Console.WriteLine(student.Name);
                    break;
                case Teacher teacher:
                    Console.WriteLine(teacher.Name);
                    Console.WriteLine(teacher.Specialization);
                    break;
                default:
                    throw new ArgumentException(
                        message: "Oject is not recognized",
                        paramName: nameof(obj));
            }
        }
    }
}

Output:

Rahul kumar
Peter
Computer Science

Next TopicC# Tuples




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf