Home >Backend Development >C++ >Can I Assign a List of Derived Class Objects to a Base Class List in C#?

Can I Assign a List of Derived Class Objects to a Base Class List in C#?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-02-02 05:31:08672browse

Can I Assign a List of Derived Class Objects to a Base Class List in C#?

C# In the C# programming language, a list of derived class objects to the base class reference list will trigger the problem of collaboration. This problem occurs when the derived category does not exist in the base class.

For example, consider the following code:

In this code, the list of the Giraffe object is assigned to the list referenced by Animal. However, this assignment failed during compilation due to the problem of coordination.
<code class="language-csharp">class Animal { }
class Giraffe : Animal { }

static void Main(string[] args)
{
    List<Giraffe> giraffes = new List<Giraffe>();
    List<Animal> animals = giraffes; // 编译时错误
}</code>

Cooperation and non -variability

C #'s generic types can be coordinated or unchanged. Coordination allows base class references to reference the derived class objects, while non -degeneration prevents such assignments. For the list, the default behavior is non -variability, which means that list cannot be assigned to list

.

Solution

In order to solve this problem, there are several solutions:

Explicit conversion:

The following code can be used to display list

to list
    :
  • However, it is not recommended to use this explicit conversion, because if the conversion is invalid, it may cause runtime errors.
Convertall method:
<code class="language-csharp">List<Animal> animals = (List<Animal>)giraffes; // 不推荐,可能导致运行时错误</code>
You can use the list

.Convertall method to convert List

to List
    by providing LAMBDA expressions that convert each element. For example:
  • Implement the iCovariant interface
<code class="language-csharp">List<Animal> animals = giraffes.ConvertAll(g => (Animal)g);</code>
It can be explicitly specified to specify the collaborative change of generic types through custom interfaces. By implementing the icovariant
    interface, the class can allow the object to use the generic type of genetic class to reference the assignment. (Note:
  • does not exist in C#, the actual solution is to use , because list itself does not support the coordination.) Change.

Which method to choose depends on the specific situation and the requirements for the robustness of the code. Method is usually the first choice, because it is safer and expresses the conversion intent. The use of is more in line with the principle of generic programming, avoiding the risk of explicit conversion. ICovariant<out T>

The above is the detailed content of Can I Assign a List of Derived Class Objects to a Base Class List in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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