public interface IDic
{
int Id { get; set; }
string Name { get; set; }
}
public class Client : IDic
{
}
How can I cast List<Client>
to List<IDic>
?
public interface IDic
{
int Id { get; set; }
string Name { get; set; }
}
public class Client : IDic
{
}
How can I cast List<Client>
to List<IDic>
?
You can't cast it (preserving reference identity) - that would be unsafe. For example:
public interface IFruit {}
public class Apple : IFruit {}
public class Banana : IFruit {}
...
List<Apple> apples = new List<Apple>();
List<IFruit> fruit = apples; // Fortunately not allowed
fruit.Add(new Banana());
// Eek - it's a banana!
Apple apple = apples[0];
Now you can convert a List<Apple>
to an IEnumerable<IFruit>
in .NET 4 / C# 4 due to covariance, but if you want a List<IFruit>
you'd have to create a new list. For example:
// In .NET 4, using the covariance of IEnumerable<T>
List<IFruit> fruit = apples.ToList<IFruit>();
// In .NET 3.5
List<IFruit> fruit = apples.Cast<IFruit>().ToList();
But this is not the same as casting the original list - because now there are two separate lists. This is safe, but you need to understand that changes made to one list won't be seen in the other list. (Modifications to the objects that the lists refer to will be seen, of course.)
List<IFruit>
which was actually a reference to a List<Apple>
. What would you expect to happen if you added a Banana
reference to that List<IFruit>
? –
Catron ToList
and Cast
(i'n use .net4). It not show in recommend. Or i missing some namespace? –
Tirzah using
directive for System.Linq
, or what you're trying to call it on, I'm not sure how you expect me to be able to help. I suggest you do more research, and if you're still stuck, you ask a question with a minimal reproducible example. –
Catron List<Apple>
to only contain apples. Therefore it's surprising when it turns out it's a banana. It would basically be a violation of a sensible type system. (Bear in mind that you may not be aware of the code that's adding a banana.) –
Catron List<IFruit> fruit = apples; fruit.Add(new Banana());
would you expect not to work? –
Catron Banana
. Basically when casting up to a List<IFruit>
it would add type checks to ensure everything was the correct type. –
Hellenic IFruit
and assign a reference to an Apple
or Banana
as the value. (I'm not sure what kind of answer you're looking for in terms of "How does that work".) –
Catron List<Apple>
IS implicitly castable to IEnumerable<IFruit>
- but not IList<IFruit>
. Why's that? –
Rhombic IEnumerable<T>
is a covariant interface, and IList<IFruit>
isn't. It's safe to convert a List<Apple>
to IEnumerable<IFruit>
because all you can do is take things out of an IEnumerable<IFruit>
, and every Apple
is an IFruit
- but with IList<T>
there's a setter, so you could write fruitList.Add(new Banana())
which clearly isn't safe. –
Catron IEnumerable
is still useful for read-only usage. –
Rhombic list.Add(new Banana());
? (If the method only needs to read from the list, it should have a parameter of type IReadOnlyList<IFruit>
instead, at which point it's fine.) –
Catron ForEach
extension method for IReadOnlyList<T>
should you wish. I'd suggest reading ericlippert.com/2009/05/18/foreach-vs-foreach before doing so though. –
Catron I too had this problem and after reading Jon Skeet's answer I modified my code from using List<T>
to use IEnumerable<T>
. Although this does not answer the OP's original question of How can I cast List<Client>
to List<IDic>
, it does avoid the need to do so and thus may be helpful to others who encounter this issue. This of course assumes that the code that requires the use of List<IDic>
is under your control.
E.g.:
public void ProcessIDic(IEnumerable<IDic> sequence)
{
// Implementation
}
Instead of:
public void ProcessIDic(List<IDic> list)
{
// Implementation
}
A Cast iterator and .ToList():
List<IDic> casted = input.Cast<IDic>().ToList()
will do the trick.
Originally I said covariance would work - but as Jon has rightly pointed out; no it won't!
And originally I also stupidly left off the ToList()
call
Cast
returns an IEnumerable<T>
, not a List<T>
- and no, covariance won't allow this conversion, because it would be unsafe - see my answer. –
Catron ToList()
was missing before reading your comment; but yes as you've shown of course Covariance won't work! Doh! –
Tedtedd Cast
call in .NET 4, so long as you specify the type argument to ToList
. –
Catron If you can use LINQ then you can do this...
List<Client> clientList = new List<Client>();
List<IDic> list = clientList.Select(c => (IDic)c).ToList();
List<Client> listOfA = new List<Client>();
List<IDic> list = listOfA.Cast<IDic>().ToList();
You can try something like:
using (var dbContext = YourDatabaseContext())
{
var list = dbContext.Clients.Where(x => x.Happy)
.OfType<IDic>()
.ToList();
}
See https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.oftype
If you don't need to modify the contents of the original list, you can implicitly convert a List into a IReadOnlyList which will let you iterate over it's contents as IDics without creating a new list.
List<Client> myClients = new List<Client>();
myClients.Add(new Client());
IReadOnlyList<IDic> castedClients = myClients;
foreach(IDic val in castedClients)
{
//do something;
}
The conversion can also occur while simply returning the list like so :
public IReadOnlyList<IDic> getClientsAsIDic()
{
return myClients;
}
Its only possible by creating new List<IDic>
and transfering all elements.
In .Net 3.5, you can do the following:
List<ISomeInterface> interfaceList = new List<ISomeInterface>(list.Cast<ISomeInterface>());
The constructor for List in this case takes an IEnumerable.
list though is only convertible to IEnumerable. Even though myObj may be convertible to ISomeInterface the type IEnumerable is not convertible to IEnumerable.
If you want to process the original list without creating a separated reference, you could define the generic method like this:
public void DoIterate<T>(List<T> myCollection) where T : IDic
{
foreach (T item in myCollection)
{
//update a property of interface
item.Name = "new Name";
}
}
Calling this method above to process the list without having to cast specific object to interface:
List<Client> clients = new List<Client>();
DoIterate(clients);
© 2022 - 2024 — McMap. All rights reserved.