Home > Backend Development > C++ > How Can I Safely Cast a `List` to a `List` in C#?

How Can I Safely Cast a `List` to a `List` in C#?

Mary-Kate Olsen
Release: 2025-01-09 22:51:46
Original
684 people have browsed it

How Can I Safely Cast a `List` to a `List` in C#?

Safely convert List to List in C#

In C#, when dealing with inheritance and generic types, you may encounter situations where you need to convert a list of derived class objects into a list of base class objects. This can cause problems if the base class contains virtual methods that are overridden in the derived class.

Consider the following example:

<code class="language-csharp">class Animal
{
    public virtual void Play(List<Animal> animals) { }
}

class Cat : Animal
{
    public override void Play(List<Animal> animals)
    {
        // Cat 特定的实现
    }
}

class Program
{
    static void Main(string[] args)
    {
        Cat cat = new Cat();
        cat.Play(new List<Cat>()); // 错误:参数 1 无法从“System.Collections.Generic.List<Cat>”转换为“System.Collections.Generic.List<Animal>”
    }
}</code>
Copy after login

When compiling this code, you will encounter the error message "Parameter 1: Cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List'". This is because, although the Cat class overrides the Play method, the method parameter type (List) remains the same.

In order to solve this problem, you need to understand the concept of generic covariance. Generic covariance allows you to safely extend the type of a parameter in a derived class override. In this case, you want to extend the parameter type from List to List.

C# 4 introduces support for generic covariance, with the help of IEnumerable and its covariance-safe interface. By declaring the Play method in the Animal class as:

<code class="language-csharp">public virtual void Play(IEnumerable<Animal> animals) { }</code>
Copy after login

Then override it in Cat class:

<code class="language-csharp">public override void Play(IEnumerable<Animal> animals) { }</code>
Copy after login

You can make the Play method covariantly safe. This allows you to pass a List to the method and then use covariance to safely convert it to an IEnumerable. The modified code looks like this:

<code class="language-csharp">class Animal
{
    public virtual void Play(IEnumerable<Animal> animals) { }
}

class Cat : Animal
{
    public override void Play(IEnumerable<Animal> animals) { }
}

class Program
{
    static void Main()
    {
        Cat cat = new Cat();
        cat.Play(new List<Cat>()); 
    }
}</code>
Copy after login

This code will compile and execute successfully without errors, allowing you to safely convert List< when a base class method is defined using IEnumerable and a derived class overrides it using IEnumerable Derived class> is converted to List.

The above is the detailed content of How Can I Safely Cast a `List` to a `List` in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template