How to convert a List<interface> to List<concrete> in C#?

How to convert a List<interface> to List<concrete> in C#?

To convert a List<Interface> to a List<Concrete> in C#, you can use either LINQ or a loop to perform the conversion. The key is to iterate through each element of the List<Interface> and cast each element to the concrete type.

Here's how you can do it using LINQ:

Suppose you have an interface IFruit and a few concrete classes that implement the interface:

public interface IFruit { void Eat(); } public class Apple : IFruit { public void Eat() { Console.WriteLine("Eating an apple."); } } public class Orange : IFruit { public void Eat() { Console.WriteLine("Eating an orange."); } } 

Now, you have a List<IFruit> that contains elements of different concrete classes:

List<IFruit> fruits = new List<IFruit> { new Apple(), new Orange(), new Apple() }; 

To convert this List<IFruit> to a List<Apple>, you can use LINQ's OfType<T>() method:

List<Apple> appleList = fruits.OfType<Apple>().ToList(); 

The OfType<Apple>() method filters the elements in the fruits list and only selects those that are of type Apple. The result is a List<Apple> containing only the Apple objects.

If you want to perform the conversion without using LINQ, you can use a loop and cast each element manually:

List<Apple> appleList = new List<Apple>(); foreach (IFruit fruit in fruits) { if (fruit is Apple apple) { appleList.Add(apple); } } 

In this loop, we iterate through each element of the fruits list and check if it is of type Apple. If it is, we cast it to Apple using the C# 7.0 syntax (declaration pattern) and add it to the appleList.

Both methods will give you a List<Apple> containing the elements that are of type Apple from the original List<IFruit>.

Examples

  1. "C# convert List of interface to List of concrete class"

    Description: This query seeks a method to convert a List of interface to a List of concrete class in C#.

    // Define your interface and concrete class interface IMyInterface { } class MyClass : IMyInterface { } // Example conversion List<IMyInterface> interfaceList = /* Your List<IMyInterface> */; List<MyClass> concreteList = interfaceList.OfType<MyClass>().ToList(); 
  2. "C# convert List of interface to List of concrete class using LINQ"

    Description: This query aims to convert a List of interface to a List of concrete class using LINQ in C#.

    // Define your interface and concrete class interface IMyInterface { } class MyClass : IMyInterface { } // Example conversion List<IMyInterface> interfaceList = /* Your List<IMyInterface> */; List<MyClass> concreteList = interfaceList.Where(item => item is MyClass).Cast<MyClass>().ToList(); 
  3. "C# convert List of interface to List of concrete class with casting"

    Description: This query is interested in converting a List of interface to a List of concrete class with casting in C#.

    // Define your interface and concrete class interface IMyInterface { } class MyClass : IMyInterface { } // Example conversion List<IMyInterface> interfaceList = /* Your List<IMyInterface> */; List<MyClass> concreteList = interfaceList.Select(item => (MyClass)item).ToList(); 
  4. "C# convert List of interface to List of concrete class without LINQ"

    Description: This query seeks a method to convert a List of interface to a List of concrete class without using LINQ in C#.

    // Define your interface and concrete class interface IMyInterface { } class MyClass : IMyInterface { } // Example conversion List<IMyInterface> interfaceList = /* Your List<IMyInterface> */; List<MyClass> concreteList = new List<MyClass>(); foreach (var item in interfaceList) { if (item is MyClass) { concreteList.Add((MyClass)item); } } 
  5. "C# convert List of interface to List of concrete class with explicit casting"

    Description: This query aims to convert a List of interface to a List of concrete class with explicit casting in C#.

    // Define your interface and concrete class interface IMyInterface { } class MyClass : IMyInterface { } // Example conversion List<IMyInterface> interfaceList = /* Your List<IMyInterface> */; List<MyClass> concreteList = interfaceList.Select(item => item as MyClass).Where(item => item != null).ToList(); 
  6. "C# convert List of interface to List of concrete class using ConvertAll method"

    Description: This query is interested in converting a List of interface to a List of concrete class using the ConvertAll method in C#.

    // Define your interface and concrete class interface IMyInterface { } class MyClass : IMyInterface { } // Example conversion List<IMyInterface> interfaceList = /* Your List<IMyInterface> */; List<MyClass> concreteList = interfaceList.ConvertAll(item => (MyClass)item); 
  7. "C# convert List of interface to List of concrete class with type check"

    Description: This query seeks a method to convert a List of interface to a List of concrete class with type checking in C#.

    // Define your interface and concrete class interface IMyInterface { } class MyClass : IMyInterface { } // Example conversion List<IMyInterface> interfaceList = /* Your List<IMyInterface> */; List<MyClass> concreteList = new List<MyClass>(); foreach (var item in interfaceList) { if (item is MyClass) { concreteList.Add((MyClass)item); } } 
  8. "C# convert List of interface to List of concrete class using Select method"

    Description: This query is interested in converting a List of interface to a List of concrete class using the Select method in C#.

    // Define your interface and concrete class interface IMyInterface { } class MyClass : IMyInterface { } // Example conversion List<IMyInterface> interfaceList = /* Your List<IMyInterface> */; List<MyClass> concreteList = interfaceList.Select(item => item as MyClass).Where(item => item != null).ToList(); 
  9. "C# convert List of interface to List of concrete class with explicit casting and LINQ"

    Description: This query seeks a method to convert a List of interface to a List of concrete class with explicit casting using LINQ in C#.

    // Define your interface and concrete class interface IMyInterface { } class MyClass : IMyInterface { } // Example conversion List<IMyInterface> interfaceList = /* Your List<IMyInterface> */; List<MyClass> concreteList = interfaceList.Select(item => item as MyClass).Where(item => item != null).ToList(); 
  10. "C# convert List of interface to List of concrete class with LINQ and Cast method"

    Description: This query is interested in converting a List of interface to a List of concrete class using LINQ and the Cast method in C#.

    // Define your interface and concrete class interface IMyInterface { } class MyClass : IMyInterface { } // Example conversion List<IMyInterface> interfaceList = /* Your List<IMyInterface> */; List<MyClass> concreteList = interfaceList.Cast<MyClass>().ToList(); 

More Tags

xelement hana distinct-values openstack-nova keylistener temp-tables spring-4 csh assertion

More C# Questions

More Date and Time Calculators

More Internet Calculators

More Weather Calculators

More Livestock Calculators