How do i convert a List<Interface> to List<Class> in c#

Ravi Kumar G N

I have an interface defined as

public interface IReaderInfo
{
    string Displayname {get;}
}

and a class that implements that interface

public class ReaderInfo : IReaderInfo
{
    string DisplayName {get;}
}

I then created a function which return List

public List<ReaderInfo> GetReaders
{
     var readers = new List<ReaderInfo>();
     var Ireaders = someobject.Getreaders();// Returns the list of IReaderInfo.
     // Now i would like cast Ireaders as readers and return.
}

How do i cast it?

grek40

You have to create a new list with casted items:

var readers = Ireaders.Cast<ReaderInfo>().ToList();

Or, if there is a possibility to have incompatible IReaderInfo entries and you only want the actual ReaderInfo objects in the result:

var readers = Ireaders.OfType<ReaderInfo>().ToList();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i convert my okhttp response to Class or List<ClassName>?

How do I convert a Json file (that contains a array) to a List (of a class object) C# Unity

How do I convert a C# List<string[]> to a Javascript array?

How do I convert a Python list into a C array by using ctypes?

How do I convert an enum to a list in C#?

How do I convert a list/dictionary into a Dataframe?

How do I convert a list of integers into bytes?

How do I convert a list into a matrix in KDB?

How do I convert list into dictionary in Python?

How do I convert a string to a list of chars?

How do I convert user input into a list?

How do I convert a list of tuples to a dictionary

How do I convert int(input) into a list

How do I convert an array or a list into a hashref?

How do I convert a Map in LIst to a Map?

How do I convert List<String?> to List<String> in .NET 6 / C# 10?

mapstruct How to convert a list of object to list of interface?

how do I convert a list of custom type to a list of String

How do I convert a list of strings to a list of dictionaries?

How do I convert a list in a .txt file to a list in Processing (python)?

How do I convert a list represented as a string to a list?

How do I convert a list of integers into an actual list in python

How do I add a class to a list?

How do I search a list that is located in a class?

Make an ArrayList of class C from a List of interface I

How can I use a list declared in two different classes in another class, without using interface in C#?

How do I convert iterator to const_iterator in my custom list iterator class?

How do I access a list in a class from within another list

How do I Map a List of Maps into a List of my Custom Class?