TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# OfType Examples

Use the OfType extension method from System.Linq to get elements of a certain type.
OfType. This method searches for elements by their types. The System.Linq namespace provides this generic method to test derived types.
Notes, usage. By using the OfType method, we get all the elements of a matching type. We do not need to cast the returned objects.LINQ
First example. To start, this example allocates an array of objects on the managed heap. It then assigns more derived types into each of the object reference elements.Object Array

Note: The OfType extension method is invoked. It uses the string type inside the angle brackets after the method call.

Extension

Result: The collection has two string elements: the two strings found in the source array.

Generic Class, Method
C# program that uses OfType extension method using System; using System.Linq; using System.Text; class Program { static void Main() { // Create an object array. object[] array = new object[4]; array[0] = new StringBuilder(); array[1] = "example"; array[2] = new int[1]; array[3] = "another"; // Filter the objects by their type. // ... Only match strings. // ... Print those strings to the screen. var result = array.OfType<string>(); foreach (var element in result) { Console.WriteLine(element); } } } Output example another
Inheritance. Consider this example: it has a base class Animal, and then derives 2 classes from it—Bird and Dog. We then use OfType to test the most derived types.InheritanceClass

Part 1: We create a List of Animal instances, all of which are either instances of the Bird or Dog classes.

Part 2: We invoke OfType to get all the Dogs in the List. We then print a property of each returned instance.

Property
C# program that uses OfType, inheritance using System; using System.Collections.Generic; using System.Linq; class Animal { } class Bird : Animal { } class Dog : Animal { public int Color { get; set; } } class Program { static void Main() { // Part 1: create List of Animal objects. var list = new List<Animal>(); list.Add(new Dog() { Color = 20 }); list.Add(new Bird()); // Part 2: use OfType to get all Dogs in the list. foreach (Dog value in list.OfType<Dog>()) { Console.WriteLine(value.Color); } } } Output 20
A discussion. What is another way of using the OfType extension? One thing you can do is invoke OfType on the collection of Forms in a Windows Forms program.

And: This enables you to locate declaratively all the form elements of a certain type, such as Button or TextBox.

Query Controls
A summary. The OfType extension is located in the System.Linq namespace in the C# language. It provides a useful way to query for elements of a certain type.
Summary, notes. As with other LINQ methods, it can be applied to various collection types. OfType provides a simple, query-oriented calling convention.
© TheDeveloperBlog.com
The Dev Codes

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