Querying for Interface Implementation

C# provides a feature you can use to query a class to find whether it implements a given interface. The operators that C# provides for this purpose is the is operator.

The is Operator

You can use the is operator to programmatically query a class and find out whether it implements a given interface, as shown in Listing 11.7.

Listing 11.7. The is Operator
 using System; public class book { public book() { } protected string book_name; public string GetData() { return book_name; } } interface Billing { bool Calculate_Discount(); } public class MyBook : book, Billing { public MyBook { } public bool Calculate_Discount() { Console.WriteLine(“Calculating discount value”); return true; } public static void Main() ...

Get Special Edition Using C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.